Gesture recognizer (swipe) on UIImageView

2019-01-22 11:55发布

问题:

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

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

}


回答2:

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.



回答3:

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


回答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;


回答5:

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


回答6:

I was running into trouble with this where I could get the UISwipeGestureRecognizer to work if I added it to self.view, but not to the individual UIImageView. Thanks Jeff Ayan for the Swift 3 code example. If you use interface builder, you can also check the User Interaction Enabled box for the UIImageView under the attributes inspector.