UITapGestureRecognizer selector, sender is the gesture, not the ui object

只谈情不闲聊 提交于 2019-12-02 16:58:53

I figured out how to get the tag, which was the most important part of the question for me. Since the gesture is the sender, I figured out the the view it is attached to is sent along with it:

[(UIGestureRecognizer *)sender view].tag

I am still curious if anyone can tell me how to send an argument through a UITapGestureRecognizer selector.

The only argument you can send through UITapGestureRecognizer selector is the UITapGestureRecognizer itself as following:

Make sure to put ":" after the selector name like you previously did :

UITapGestureRecognizer *singleTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];

Then add a parameter to selectImage so you can retrieve the View as following:

-(void) selectImage:(UITapGestureRecognizer *)gestureRecognizer{

    //Get the View
    UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view;
}

From @dysan819 answer, I manage to get object without tag. In my case is UILabel.

- (void)labelTap:(id)sender {
    NSLog(@"tap class: %@", [[(UIGestureRecognizer *)sender view] class]);
    if ([[(UIGestureRecognizer *)sender view] isKindOfClass:[UILabel class]]) {
        UILabel *lb = (UILabel*)[(UIGestureRecognizer *)sender view];
        NSLog(@"tap: %@", lb.text);
    }
}

If you need distinct functionality for the handler you might check out the BlocksKit project and this file in particular. The project is a CocoaPods project so you can install it easily into your toolchain.

An example from the first referenced code file:

UITapGestureRecognizer *singleTap = [UITapGestureRecognizer recognizerWithHandler:^(id sender) {
     NSLog(@"Single tap.");
 } delay:0.18];
 [self addGestureRecognizer:singleTap];

This could effectively allow you to setup a gesture recognizer easily for each image.

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