iOS点击手势状态开始没有命中

我用点击手势识别器制作了一个可点击的视图,工作得很好。 但我希望在触摸发生时突出显示视图,并在触摸结束时将其移除。

我试过这个:

- (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]; } } 

“Ended确实”的NSLog显示但是开始没有显示,所以它永远不会被选中。 为什么是这样?

UITapGestureRecognizer永远不会进入UIGestureRecognizerStateBegan状态。 只有连续手势(例如滑动或捏合)才会使识别器从UIGestureRecognizerStatePossible变为UIGestureRecognizerStateBegan离散手势(例如点击)将其识别器直接放入UIGestureRecognizerStateRecognized ,即单击一下,直接进入UIGestureRecognizerStateEnded

也就是说,也许你正在寻找一个UILongPressGestureRecognizer ,这是一个连续的识别器,它将进入UIGestureRecognizerStateBegan ,让你能够识别触摸的开始和结束?

可能为时已晚。 但是,如果您严格要使用Gesture识别器,这也会对您有所帮助。

  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]; } } 

您还可以使用touchesBegan:withEvent: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 } }