检测发送者操作方法内 UIButton 内点击/按下的位置 [英] Detect what is the location of the tap/press inside UIButton inside sender action method

查看:25
本文介绍了检测发送者操作方法内 UIButton 内点击/按下的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIButton 拉伸到视图的大小(覆盖视图/屏幕).

I have a UIButton stretched to the size of the view (covers the view/screen).

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

I have set a Touchdown action and I need in this action to find out what is the location of the tap/press on the button (get x and y coordinates).

有可能吗?

推荐答案

使用 senderevent 参数创建动作函数.然后你就可以得到按钮上的触摸位置了.

Create the action function with sender and event params. Then you can get the touch location on the button.

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

这篇关于检测发送者操作方法内 UIButton 内点击/按下的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆