IOS Tap Gesture Status Does Not Hit

I made a mouse click using a gesture recognizer that works just fine. But I want to highlight the view when the touch happens, and delete it when the touch ends.

I tried this:

- (IBAction)refresh:(UITapGestureRecognizer *)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]; } } 

NSLog "Ended does" is displayed, but started not so that it never gets into the selected one. Why is this?

+7
ios cocoa-touch
source share
3 answers

UITapGestureRecognizer will never go into the UIGestureRecognizerStateBegan state. Only continuous gestures (for example, napkins or a pinch) will cause their recognizers to move from UIGestureRecognizerStatePossible to UIGestureRecognizerStateBegan . Discrete gestures, such as a tap, place their recognizers directly in the UIGestureRecognizerStateRecognized , that is, for one tap, directly in the UIGestureRecognizerStateEnded .

However, perhaps you are looking for a UILongPressGestureRecognizer , which is a continuous recognizer that will go into the UIGestureRecognizerStateBegan , which will allow you to distinguish between the start and end of the touch?

+10
source share

Perhaps too late. But it will also help you if you strictly want to use a 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]; } } 
+5
source share

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

 - (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 } } 
+2
source share

All Articles