从情节提要板设置IBAction时获得“触摸向内拖动"距离 [英] Get 'Touch Drag Inside' Distance When Setting IBAction From Storyboard

查看:57
本文介绍了从情节提要板设置IBAction时获得“触摸向内拖动"距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何设置UIPanGesture事件和识别器并以编程方式创建它们时获取所需的数据.

I know how to set UIPanGesture events and recognizers and get the data I need when I create them programatically.

但是我试图学习如何使用情节提要和使用IBOutlets.

However I am trying to learn how to work with the storyboard and use IBOutlets.

我已将IBAction拖到触摸内部拖动"中.

I have dragged over an IBAction for 'Touch Drag Inside'.

@IBAction func dragButtonDragEvent(sender: AnyObject)
    {
        println(sender)
    }

问题是我得到的发件人对象仅仅是按钮本身.

The problem is the sender object I get is just the button itself.

使用IBAction时是否可以获取拖动距离或拖动事件数据?

Is there a way to get the drag distance or drag event data when using an IBAction?

我尝试了

@IBAction func dragButtonDragEvent(sender: AnyObject)
    {
        var drag:UIPanGestureRecognizer = sender as UIPanGestureRecognizer
        println(drag.state)
    }

但是我收到一个严重的内存访问错误,并且由于发送方不是事件,所以未执行println.

But I get a bad memory access error and the println is not executed as the sender is the button NOT the event.

您能否建议从情节提要板连接时如何使用内部触摸拖动事件?

Can you suggest how to use Touch Drag Inside events when connecting from a Storyboard?

推荐答案

使用事件参数创建@IBAction函数,如下所示:

Create the @IBAction function with an event parameter like this:

@IBAction func touchDragInsideAction(sender: AnyObject, event: UIEvent) {
    // if the sender is a button
    if let button = sender as? UIButton {
        // get the touch inside the button
        let touch = event.touchesForView(button)?.anyObject() as UITouch
        // println the touch location
        println(touch.locationInView(button))
    }
}

当用户在按钮上拖动手指时,这将在用户的触摸位置上打印.然后,您可以使用第一个点和最后一个点来获取距离.

This will println the user's touch location on the button as they drag their finger across the button. You can then use the first point and last point to get the distance.

请注意,如果要修改现有功能的方法签名,则必须在情节提要中重新连接它.

Please note, if you're modifying the method signature of the existing function, then you'll have to reconnect it in the storyboard.

这篇关于从情节提要板设置IBAction时获得“触摸向内拖动"距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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