35

当我使用此代码在 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");
}
4

6 回答 6

102

启用默认禁用的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");   
    } 

}
于 2013-07-29T15:22:09.920 回答
9

在斯威夫特 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")
    }
}
于 2017-01-03T14:18:10.037 回答
8

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

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

请参阅此处的文档。

于 2013-07-29T15:13:51.633 回答
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
       }
    }
于 2018-05-25T04:54:41.473 回答
2

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

imageView.userInteractionEnabled = YES;
于 2013-07-29T15:12:52.020 回答
0

我遇到了麻烦,如果我将 UISwipeGestureRecognizer 添加到 self.view,而不是单个 UIImageView,我可以让它工作。感谢 Jeff Ayan 提供的 Swift 3 代码示例。如果您使用界面生成器,您还可以在属性检查器下选中 UIImageView 的 User Interaction Enabled 框。

于 2017-03-22T16:15:20.167 回答