【问题标题】:Gesture recognizer (swipe) on UIImageViewUIImageView 上的手势识别器(滑动)
【发布时间】:2013-07-29 15:11:22
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c cocoa uiimageview uigesturerecognizer


    【解决方案1】:

    启用 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 不是更有效吗?
    • 是的,您可以...以上代码仅用于表示目的。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      请参阅文档here

      【讨论】:

        【解决方案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
               }
            }
        

        【讨论】:

          【解决方案5】:

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

          imageView.userInteractionEnabled = YES;
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-07-20
              • 1970-01-01
              • 1970-01-01
              • 2016-08-04
              • 2015-07-23
              • 1970-01-01
              相关资源
              最近更新 更多