Gesture recognizer (swipe) on UIImageView

后端 未结 6 2061
栀梦
栀梦 2020-12-14 06:19

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

- (voi         


        
相关标签:
6条回答
  • 2020-12-14 06:49

    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")
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:54

    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.

    0 讨论(0)
  • 2020-12-14 06:54

    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.

    0 讨论(0)
  • 2020-12-14 06:59

    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");   
        } 
    
    }
    
    0 讨论(0)
  • 2020-12-14 07:05

    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
           }
        }
    
    0 讨论(0)
  • 2020-12-14 07:06

    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;
    
    0 讨论(0)
提交回复
热议问题