UITapGestureRecognizer initWithTarget:action: method to take arguments?

后端 未结 2 971
太阳男子
太阳男子 2021-02-04 11:32

I\'m using UITapGestureRecognizer because I\'m using a UIScrollView that acts as a container for my UILabels. Basically I\'m trying to use

相关标签:
2条回答
  • 2021-02-04 12:03

    You can use just one UITapGestureRecognizer and in your gesture handler (your myLaberXTap), which has the syntax:

     - (void)handleGesture:(UITapGestureRecognizer*)gestureRecognizer {
         ...
     } 
    

    use gesture.view to know which view you are working on.

    0 讨论(0)
  • 2021-02-04 12:18

    Add a single gesture recognizer to the view that is the superview of your various labels:

    UITapGestureRecognizer *myLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabelTapHandler:)];
    [myLabelParent addGestureRecognizer:myLabelTap];
    

    Then when you handle the gesture, determine which label was tapped:

    -(void)myLabelTapHandler:(UIGestureRecognizer *)gestureRecognizer {
        UIView *tappedView = [gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view] withEvent:nil];
    
        // do something with it
    }
    
    0 讨论(0)
提交回复
热议问题