Gesture recognizer (swipe) on UIImageView

41,502

Solution 1

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");   
    } 

}

Solution 2

In Swift 3

ImageView.isUserInteractionEnabled = true
let 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")
    }
}

Solution 3

Its using swift 4, images are picked from your photo library can be swiped over the image view using swipe gesture.

   @IBOutlet weak var Imageview: UIImageView!

   var images=[UIImage]()
   var i=Int()
   override func viewDidLoad() {
     super.viewDidLoad()

     let swipeLeftGesture=UISwipeGestureRecognizer(target: self, action: #selector(SwipeLeftImage))
     Imageview.isUserInteractionEnabled=true
     swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.left
     Imageview.addGestureRecognizer(swipeLeftGesture)

     let swipeRightGesture=UISwipeGestureRecognizer(target: self, action: #selector(SwipeRightImage))
     swipeRightGesture.direction = UISwipeGestureRecognizerDirection.right
     Imageview.addGestureRecognizer(swipeRightGesture)
    }
    @objc func SwipeLeftImage(){
      if i<images.count-1{
        i+=1
        Imageview.image=images[i]
      }
    }
    @objc func SwipeRightImage(){
      if i<=images.count-1{
        i-=1
        Imageview.image=images[i]
      }
    }
//MARK:- imagePickerController Delegate Function
   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      self.dismiss(animated: true, completion: nil)
      if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        Imageview.contentMode = .scaleToFill
        Imageview.image=nil
        images.insert(pickedImage, at: images.endIndex)
        Imageview.image = pickedImage
       }
    }

Solution 4

Different than other UIViews, UIImageView, as default, comes with user interaction disabled. Include this line in your code and the gesture should work as expected:

imageView.userInteractionEnabled = YES;
Share:
41,502
Sucrenoir
Author by

Sucrenoir

RoR, Node.js Cocoa dev

Updated on July 09, 2022

Comments

  • Sucrenoir
    Sucrenoir almost 2 years

    I am trying to NSLog when I swipe over an UIImageView with this code, but it does not work for some reason. Any idea ?

    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIImage *image = [UIImage imageNamed:@"image2.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
        imageView.frame = [[UIScreen mainScreen] bounds];
    
        [self.view addSubview:imageView];
    
        UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
        [recognizer setNumberOfTouchesRequired:1];
        [imageView addGestureRecognizer:recognizer];
    
    }
    
    - (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
        NSLog(@"right swipe");
    }