iOS Tap gesture state begin doesn't hit

前端 未结 5 1051
半阙折子戏
半阙折子戏 2021-01-12 13:56

I have made a view clickable with a tap gesture recognizer, which is working just fine. But I want to highlight the view when the touch happens en remove it when the touch e

相关标签:
5条回答
  • 2021-01-12 14:24

    UITapGestureRecognizer will never go in the UIGestureRecognizerStateBegan state. Only continuous gestures (such as a swipe or a pinch) will result for their recognizers going from UIGestureRecognizerStatePossible to UIGestureRecognizerStateBegan. Discrete gestures, such as a tap, put their recognizers directly into UIGestureRecognizerStateRecognized, i.e. for a single tap, right into UIGestureRecognizerStateEnded.

    That said, maybe you're looking for a UILongPressGestureRecognizer, which is a continuous recognizer that will enter UIGestureRecognizerStateBegan, allowing you to discern beginning and end of touch?

    0 讨论(0)
  • 2021-01-12 14:34

    It might be too late. But this will also help you, if you strictly want to use Gesture recognizer.

       UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(refresh:)];
        longPress.minimumPressDuration = 0.0;
    
     - (IBAction)refresh:(UILongPressGestureRecognizer *)sender {
         if(self.currentStatus == NODATA){
           if(sender.state == UIGestureRecognizerStateBegan){
             NSLog(@"Began!");
             [self.dashboardViewController.calendarContainer state:UIViewContainerStatusSELECTED];
           }
           if (sender.state == UIGestureRecognizerStateEnded){
             NSLog(@"%@", @"Ended");
            [self.dashboardViewController.calendarContainer state:UIViewContainerStatusNORMAL];
           } 
           [self setState:REFRESHING data:nil];
        }
       }
    
    0 讨论(0)
  • 2021-01-12 14:35

    Swift 4.2

    0 讨论(0)
  • 2021-01-12 14:44

    Quite an old question, but still. Hope it will be helpful for somebody. If I've got the author's question right, the idea is to detect whether the tap has began recognizing and to perform an action. Like if you don't want target to trigger only when the user releases the finger but already when he touch it for the very first time.

    An easy way to do so is to make an extension for UITapGestureRecognizer like below:

    fileprivate class ModTapGestureRecognizer: UITapGestureRecognizer {
        
        var onTouchesBegan: (() -> Void)?
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
            onTouchesBegan?()
            super.touchesBegan(touches, with: event)
        } 
    }
    

    Later in your code you can use it like so:

    let tapRecognizer = ModTapGestureRecognizer()
    tapRecognizer.addTarget(self, action: #selector(didTapped))
    tapRecognizer.onTouchesBegan = {
       print("Yep, it works")
    }
    yourView.addGestureRecognizer(tapRecognizer)
    

    And you're awesome!

    0 讨论(0)
  • 2021-01-12 14:45

    You can also use touchesBegan:withEvent: and touchesEnded:withEvent: methods.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *t = [event touchesForView:_myView];
        if([t count] > 0) {
            // Do something 
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *t = [event touchesForView:_myView];
        if([t count] > 0) {
            // Do something
        }
    }
    
    0 讨论(0)
提交回复
热议问题