【问题标题】:Obtain location of UIButton press in swift快速获取 UIButton 按下的位置
【发布时间】:2016-04-30 07:43:27
【问题描述】:

我在视图上设置了几个鼓垫,它们都连接到一个 drumPadPlay IBAction 方法。我想确定用户在发送器按钮内按下的 y 轴位置,以调整鼓垫播放的音量。我正在使用 swift。

在将其标记为重复问题之前,我已经引用了这个问题,并且该解决方案不适用于当前版本的 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 !并搜索了发件人的属性/功能列表,但找不到任何看起来有帮助的东西。

【问题讨论】:

  • 您链接到的答案是正确的。也许您可以编辑您的问题以显示您尝试过的代码以及您遇到的错误/问题。
  • 我编辑了问题以包含代码示例。我觉得这是一个如此简单的答案,另一个答案假设它知道用什么替换 buttonView 但我无法弄清楚。

标签: ios xcode swift uibutton


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2017-08-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多