2016-01-23 46 views
1

我在视图中设置了几个drumpad,它们都连接到drumPadPlay IBAction方法。我想确定发件人按钮内用户按下的y轴位置,以调整鼓垫将播放的音量。我正在使用swift。在swift中获取UIButton的位置

在这被标记为重复问题之前,我引用了这个问题,该解决方案并不适用于当前版本的swift,或者我不知道该如何回答“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 
} 

这里是我的代码 - 错误是“使用未解决的标识符‘buttonView’” 我不知道用什么buttonView。我曾尝试sender.superview!并通过发件人的属性/功能列表进行搜索,并找不到任何看起来会有所帮助的内容。

+1

您链接到的答案是正确的。也许你可以编辑你的问题,以显示你尝试的代码和你有错误/问题。 – Paulw11

+0

我编辑了问题以包含代码示例。我有一种感觉,这是一个简单的答案,另一个答案假设它将知道什么来替换buttonView,但我无法弄清楚。 – evansjohnson

回答

1

senderBNDrumPad这是(大概)UIView的子类,所以在这种情况下,您将只使用sender。在你连接的答案中,sender参数被定义为AnyObject,因此他们必须首先将其转换为UIView

@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 
}