ios UIImageView 上的手势识别器(滑动)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17927688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:33:15  来源:igfitidea点击:

Gesture recognizer (swipe) on UIImageView

iosobjective-ccocoauiimageviewuigesturerecognizer

提问by Sucrentheitroad

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

当我使用此代码在 UIImageView 上滑动时,我正在尝试使用 NSLog,但由于某种原因它不起作用。任何的想法 ?

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

回答by icodebuster

Enable UIImage view user interactionwhich is disabled by default.

启用默认禁用的UIImage 视图用户交互

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

}

回答by jeff ayan

In Swift 3

在斯威夫特 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")
    }
}

回答by jeff ayan

Be sure to add imageView.userInteractionEnabled = YES;after you create your UIImageView.

请务必imageView.userInteractionEnabled = YES;在创建 UIImageView 后添加。

This allows users to interactwith your view such as a tap, drag, swipe or other general gestures.

这允许用户与您的视图进行交互,例如点击、拖动、滑动或其他常规手势。

See the documentation here.

请参阅此处的文档。

回答by Kawalpreet Kaur

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

它使用 swift 4,从您的照片库中挑选的图像可以使用滑动手势在图像视图上滑动。

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

回答by Lucas Eduardo

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:

比其他的不同UIViewsUIImageView作为默认情况下,带有用户交互disabled。在您的代码中包含此行,手势应按预期工作:

imageView.userInteractionEnabled = YES;

回答by Wyatt

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.

我遇到了这个问题,如果我将它添加到 self.view 中,我可以让 UISwipeGestureRecognizer 工作,但不能添加到单个 UIImageView 中。感谢 Jeff Ayan 提供 Swift 3 代码示例。如果您使用界面构建器,您还可以在属性检查器下选中 UIImageView 的用户交互启用框。