[UITapGestureRecognizer tag]: unrecognized selector sent to instance

前端 未结 5 1423
一个人的身影
一个人的身影 2020-12-16 00:44

I am having a series of imageview arranged, and assigning a TapView recognizer to it

UITapGestureRecognizer *tapRecognizer = [[UITa         


        
相关标签:
5条回答
  • 2020-12-16 01:04

    Just Change your Selector Method with the following..and it will work

    tapgesture will have the whole view which is tapped.. and then you can get the tag property from it as i have stated in following

    -(void)action:(UITapGestureRecognizer *)tapGesture{
    
         NSLog(@"TESTING TAP");
            NSLog (@"%d",tapGesture.view.tag);
    
        }
    
    0 讨论(0)
  • 2020-12-16 01:08

    Neither UITapGestureRecognizer nor UIGestureRecognizer declares a property or method called tag.

    You can't use it. That's why you're getting the error.

    On a related note. I really don't like using tag in general. There is always a better way to do what you're doing without using tag.

    0 讨论(0)
  • 2020-12-16 01:08

    Swift equivalent of the accepted answer:

    //use this to set up your tap gesture
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.reactToTap(sender:)))
    imageView.tag = 33
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGesture)
    

    This is the function which gets triggered by the tap:

    @objc private func reactToTap(sender: UITapGestureRecognizer) {
                
        print("ImageView tag: ", sender.view?.tag ?? 0) //"ImageView tag: 33"
    }
    
    0 讨论(0)
  • 2020-12-16 01:14

    You cannot get tag property of UITapGestureRecognizer rather you have to get of its view's property,

    You can try,

    -(void)action:(id)sender
      {
         NSLog(@"TESTING TAP");
         NSLog (@"%d",[[sender view]tag]);
    
      }
    
    0 讨论(0)
  • 2020-12-16 01:20

    You can use this..

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(action:)];
    [tapRecognizer setNumberOfTouchesRequired:1];
    [tapRecognizer setDelegate:self];
    imageView.userInteractionEnabled = YES;
    imageView.tag = 1111;
    [imageView addGestureRecognizer:tapRecognizer];
    

    And in action try this..

    -(void) action:(id)sender
      {
        NSLog(@"TESTING TAP");
        UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
        NSLog (@"%d",[tapRecognizer.view tag]);
      }
    

    Explaination:

    UITapGestureRecognizer has not property like tag. but it has property view, from this property you can access the view with which UITapGestureRecognizer was attached.

    Hope it will help you

    0 讨论(0)
提交回复
热议问题