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

一笑奈何 提交于 2019-12-05 02:06:20

问题


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];

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!


回答1:


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


来源:https://stackoverflow.com/questions/6835233/referencing-a-button-when-the-actions-sender-is-a-uigesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!