当操作的发送者是UIGestureRecognizer时引用按钮 [英] Referencing a button when the action's sender is a UIGestureRecognizer

查看:128
本文介绍了当操作的发送者是UIGestureRecognizer时引用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为myButton的按钮,并且给了它一个UIGestureRecognizer,以便仅当用两根手指按下myButton时,才会运行IBAction:

I've got a button called myButton and I gave it a UIGestureRecognizer so that an IBAction is only run when myButton is pressed with two fingers:

UIGestureRecognizer  *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerTap:)];
[(UITapGestureRecognizer *) tapper setNumberOfTouchesRequired:1];
[newTaskButton addGestureRecognizer:tapper];

在添加手势识别器之前,我可以使用sender引用刚刚按下的按钮,但是现在sender现在是手势识别器.我仍然需要引用被按下的按钮...是否有任何方法可以做到这一点?一个简单的方法可能返回使用手势识别器的所有内容?谢谢!

Prior to adding the gesture recognizer, I could use sender to reference the button that was just pressed, however now sender is now the gesture recognizer. I still need to reference the button that was pressed...Is there any way to go about doing so? An easy method that returns whatever is using the gesture recognizer maybe? Thanks!

推荐答案

UIGestureRecognizer类具有view属性,该属性表示手势识别器附加到按钮的视图.

The UIGestureRecognizer class has a view property representing the view the gesture recognizer is attached to, in your case, the button.

- (void)twoFingerTap:(UIGestureRecognizer *)sender {
    UIButton *myButton = (UIButton *)sender.view;
}

这篇关于当操作的发送者是UIGestureRecognizer时引用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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