当操作的发送者是UIGestureRecognizer时引用按钮 [英] Referencing a button when the action's sender is a 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屋!