开发者

Referencing a button when the action's sender is a UIGestureRecognizer

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 = [[U开发者_运维技巧ITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerTap:)];
[(UITapGestureRecognizer *) tapper setNumberOfTouchesRequired:1];
[newTaskButton addGestureRecognizer:tapper];

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!


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜