UITapGestureRecognizer initWithTarget:action: method to take arguments?

后端 未结 2 975
太阳男子
太阳男子 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: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
    }
    

提交回复
热议问题