Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 检测点击/按下inside UIButton inside sender动作方法的位置_Swift_Uiview_Uibutton - Fatal编程技术网

Swift 检测点击/按下inside UIButton inside sender动作方法的位置

Swift 检测点击/按下inside UIButton inside sender动作方法的位置,swift,uiview,uibutton,Swift,Uiview,Uibutton,我有一个ui按钮拉伸到视图的大小(覆盖视图/屏幕) 我已经设置了一个触地动作,我需要在这个动作中找出点击/按下按钮的位置(获得x和y坐标) 有可能吗?使用发送方和事件参数创建操作函数。然后您可以在按钮上获得触摸位置 @IBAction func buttonAction(sender: AnyObject, event: UIEvent) { // downcast sender as a UIView let buttonView = sender as UIView;

我有一个
ui按钮
拉伸到视图的大小(覆盖视图/屏幕)

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


有可能吗?

使用
发送方
事件
参数创建操作函数。然后您可以在按钮上获得触摸位置

@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 3接受答案的版本:

@IBAction func increaseButtonTapped(sender: UIButton, event: UIEvent) {

    // get any touch on the buttonView

    let events = event.touches(for: sender)

    for event in events! {

        let location = event.location(in: sender)

        print("\(location)")

    }

}

我尝试过,给出了以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[beater.ViewController userbeat:]:未识别的选择器发送到实例0x15853200”知道可能是什么吗?代码中有东西试图调用类
beater
中名为
userbeat
的函数。但是,函数
userbeat
不存在,或者已解除锁定
beater
对象。@IBAction func userbeat(发送方:AnyObject,事件:UIEvent){let buttonView=sender as UIView;//如果let touch=event,则在buttonView上进行任何触摸。touchsforview(buttonView)?.anyObject()作为?UITouch{//在按钮println上打印触摸位置(“这是什么(touch.locationInView(buttonView)))}}噢,哈哈,粘贴不太好,不知道有什么可能出错,因为它们都存在。。