UITapGestureRecognizer发件人是手势,而不是ui对象 [英] UITapGestureRecognizer sender is the gesture, not the ui object

查看:131
本文介绍了UITapGestureRecognizer发件人是手势,而不是ui对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用的按钮,我给它一个UIGestureRecognizer,这样只有长按按钮才会运行IBAction。您可以通过向按钮自身添加UILongPressGestureRecognizer来完成此操作。然后你控制将手势识别器拖动到这样的函数:

I've got a button called and I gave it a UIGestureRecognizer so that an IBAction is only run when the button is long pressed. You do this by adding a UILongPressGestureRecognizer to the button iteself. Then you control drag that gesture recognizer to a function like this:

 @IBAction func handleGesture(sender: UIGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Began
    {
        let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:")
        let alertController = UIAlertController(title: "**I want this to be the title of the button**", message:
            "This is the description of the function you pressed", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

如您所见,UIAlertController出现,我希望标题显示当前标题的按钮。如果此函数的发件人是按钮,我可以使用sender.currentTitle调用它。当函数的发送者是手势时,如何获得与手势相关联的按钮的标题。

As you can see the UIAlertController appears, I want the title to show the buttons current title. If the sender of this function was the button, I could call this with sender.currentTitle. How can I get the title of the button that is tied to the gesture, when the sender of the function is the gesture.

我在目标C中看过这样的帖子,但在Swift上没有。当你长按按钮本身时,这就是项目获取按钮描述的全部内容。如果有人有任何想法如何用我迄今为止所做的完成,或者有不同的方法,请告诉我。

I have seen posts like this in objective C, but nothing really on Swift. This is all for a project to get a buttons description when you longpress on the button itself. If anyone has any ideas how to accomplish this with what i've done so far, or have a different method, please tell me.

谢谢!

推荐答案

您可以获得对视图的引用手势通过其视图属性添加到。在这种情况下,您将其添加到按钮,以便视图属性将返回给您按钮。

You can get a reference to the view the gesture is added to via its view property. In this case you are adding it to the button so the view property would return you you the button.

let button = sender.view as? UIButton

这篇关于UITapGestureRecognizer发件人是手势,而不是ui对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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