Can you attach a UIGestureRecognizer to multiple views?

前端 未结 12 1015
忘了有多久
忘了有多久 2020-11-22 14:08
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];         


        
12条回答
  •  再見小時候
    2020-11-22 15:10

    I got around it by using the below.

    for (UIButton *aButton in myButtons) {
    
                UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
                longPress.minimumPressDuration=1.0;
                [aButton addGestureRecognizer:longPress];
                [longPress release];
    
    }
    

    Then in my handleLongPress method I just set a UIButton equal to the view of the gesture recognizer and branch what I do based upon that button

    - (void)handleLongPress:(UILongPressGestureRecognizer*)gesture {
        if ( gesture.state == UIGestureRecognizerStateEnded ) {
            UIButton *whichButton=(UIButton *)[gesture view];
            selectedButton=(UIButton *)[gesture view];
        ....
    }
    

提交回复
热议问题