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

35
我正在尝试使用以下代码在滑过UIImageView时进行NSLog,但出于某种原因它没有起作用。 有任何想法吗?

我正在尝试使用以下代码在滑过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");
}
6个回答

102

默认情况下,UIImage视图的用户交互是被禁用的。开启它:

Enable UIImage视图的用户交互,默认情况下是被禁用的。

[imageView setUserInteractionEnabled:YES];

添加滑动手势事件

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];

处理滑动手势事件

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");   
    } 

}

handleSwipe函数中使用else if会不会更加高效呢? - shadowmoses
是的,你可以...上面的代码只是为了表示目的。 - icodebuster

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

8
请在创建UIImageView后添加imageView.userInteractionEnabled = YES;
这将允许用户与您的视图进行交互,如点击、拖动、滑动或其他常见手势。
请参阅此处的文档。

4

该应用使用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
       }
    }

2
与其他UIViews不同,UIImageView默认情况下用户交互是disabled的。在您的代码中包含此行,手势应该按预期工作:
imageView.userInteractionEnabled = YES;

0
我在编程中遇到了麻烦,如果我将UISwipeGestureRecognizer添加到self.view中,它可以正常工作,但是如果添加到单独的UIImageView上,则无法正常工作。感谢Jeff Ayan提供的Swift 3代码示例。 如果您使用界面生成器,您还可以在属性检查器下为UIImageView勾选“用户交互启用”框。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接