Skip to content
Search

IOS:UITapGestureRecognizer

lastmod 2018-08-17created 2014-03-063 edits

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

Gesture Event on UIView

This is how to register various touch gestures on a plain UIView rather than a Button. As far as I know, it is supported since iOS sdk4.0.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPiece:)];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;

Just create a UITapGestureRecognizer and specify the action. Then register it on the view with addGestureRecognizer, and the event is registered on that view.

  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer

As shown above, there are many events besides Tab, and using them well should make event handling easy

Gesture Event on UIImageVIew

If you want to apply it to UIImageView as well, you need to add the property below.

myImageView.userInteractionEnabled = YES;

Favorite site