swift - 在发送者操作方法中检测 UIButton 中点击/按下的位置是什么

标签 swift uiview uibutton

我有一个 UIButton 拉伸(stretch)到 View 的大小(覆盖 View /屏幕)。

我已经设置了一个 Touchdown Action ,我需要在这个 Action 中找出点击/按下按钮的位置(获取 x 和 y 坐标)。

这可能吗?

最佳答案

使用 senderevent 参数创建 Action 函数。然后就可以得到按钮上的触摸位置。

@IBAction func buttonAction(sender: AnyObject, event: UIEvent) {
    // downcast sender as a UIView
    let buttonView = sender as UIView;

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

关于swift - 在发送者操作方法中检测 UIButton 中点击/按下的位置是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994399/

相关文章:

ios - Voice Over 不关注 UICollectionView

swift - 如何在 swift 中向 https 请求添加正文

ios - 将 CAShapeLayer 应用于除 UIButton 之外的主 UIView

iphone - 添加 tableview 作为 subview (如果你必须从 UITableVC 继承)

ios - 按下 UITableViewCell 内的按钮时的 scrollToRowAtIndexPath

ios - 使用 addTarget : action: forControlEvents: 将 indexPath.row 移动到标签

swift - 一次到达 NSTextField 的两个 IBOutlet

ios - 如何快速将base64转换为NSDATA

ios - 我的自定义 UIView 未显示在屏幕上

ios - Apple 会批准那里的 UIButtons 吗?