1

I have several drumpads set up on my view and they all connect to a drumPadPlay IBAction method. I would like to determine the y-axis location of the user's press inside the sender button to adjust the volume the drum pad will play at. I'm using swift.

Before this is flagged as a duplicate question I have referenced this question and the solution did not work for the current version of swift, or perhaps I don't know what to put where the answer says "buttonView". Detect what is the location of the tap/press inside UIButton inside sender action method

@IBAction func drumPadPressed(sender: BNDrumPad, event: UIEvent) {

    //...code for function here

    // get any touch on the buttonView
    if let touch = event.touchesForView(sender.)?.anyObject() as? UITouch {
        // print the touch location on the button
        println(touch.locationInView(buttonView))
    }

    //...more code for function here
}

Here is my code - the error is "use of unresolved identifier 'buttonView'" I don't know what to use for buttonView. I have tried sender.superview! and searched through sender's list of properties/functions and couldn't find anything that looked like it would help.

Community
  • 1
  • 1
evansjohnson
  • 452
  • 4
  • 14
  • 1
    The answer you linked to is correct. Perhaps you could edit your question to show the code you tried and the error/problem you have. – Paulw11 Jan 23 '16 at 20:17
  • I edited the question to include the code example. I have a feeling it's such a simple answer the other answer assumed it would be known what to replace buttonView with but I can't figure it out. – evansjohnson Jan 23 '16 at 21:13

1 Answers1

1

sender is a BNDrumPad which is (presumably) a subclass of a UIView, so in this case you will just use sender. In the answer you linked to, the sender parameter was defined as AnyObject so they had to cast it to a UIView first.

@IBAction func drumPadPressed(sender: BNDrumPad, event: UIEvent) {

    //...code for function here

    // get any touch on the buttonView
    if let touch = event.touchesForView(sender)?.anyObject() as? UITouch {
        // print the touch location on the button
        println(touch.locationInView(buttonView))
    }

    //...more code for function here
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186