Gesture recognizer (swipe) on UIImageView
Enable UIImage view user interaction which is disabled by default.
[imageView setUserInteractionEnabled:YES];
Adding a Swipe Gesture Events
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];// Setting the swipe direction.[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];// Adding the swipe gesture on image view[imageView addGestureRecognizer:swipeLeft];[imageView addGestureRecognizer:swipeRight];
Handling Swipe Gesture Events
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"Left Swipe"); } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"Right Swipe"); } }
In Swift 3
ImageView.isUserInteractionEnabled = truelet swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.getSwipeAction(_:)))self.ImageView.addGestureRecognizer(swipeGesture) func getSwipeAction( _ recognizer : UISwipeGestureRecognizer){ if recognizer.direction == .right{ print("Right Swiped") } else if recognizer.direction == .left { print("Left Swiped") }}
Be sure to add imageView.userInteractionEnabled = YES;
after you create your UIImageView.
This allows users to interact with your view such as a tap, drag, swipe or other general gestures.
See the documentation here.