Snowman
Snowman

Reputation: 32091

UIGestureRecognizer tap and remove

I have a UILongPressGestureRecognizer that fires the method -(void)didPress. The current view has by default a UIImage. didPress makes the UIImage disappear off the screen. However, the image should only disappear off the screen as long as the user is still long pressing. Once the user lets go of the initial long press, the image reappears. I have the code already to handle the initial long press, but I'm not sure how to determine when the user has let go of that long touch.

Upvotes: 1

Views: 544

Answers (1)

Herz Rod
Herz Rod

Reputation: 831

You should pass the UILongPressGestureRecognizer to your method and check the state of the gesture, when the user releases his finger/fingers out of the screen, it will send a state of UIGestureRecognizerStateEnded

- (void)didPress:(UILongPressGestureRecognizer *)recognizer {
   if (recognizer.state == UIGestureRecognizerStateBegan) {
     NSLog(@"Started");
   }
   if (recognizer.state == UIGestureRecognizerStateEnded) {
     NSLog(@"Finished");    
   }
}

Using touchesBegan:withEvent: is old and way before UIGestureRecognizer's

Upvotes: 4

Related Questions