视图未检测到滑动图像库的手势识别器

Posted

技术标签:

【中文标题】视图未检测到滑动图像库的手势识别器【英文标题】:View is not detecting Gesture Recognizer for swipe image gallery 【发布时间】:2020-04-27 20:49:09 【问题描述】:

我想在我的CustomShareExtensionViewController 中添加一个SwipeImageGallery,但现在它没有检测到gestures,我不知道为什么。

图片:

let images: [UIImage] = [
            UIImage(named: "avocadoImage")!,        // 0
            UIImage(named: "beerImage")!,           // 1
            UIImage(named: "bikeImage")!,           // 2
            UIImage(named: "christmasImage")!,      // 3
            UIImage(named: "dressImage")!,          // 4
            UIImage(named: "giftImage")!,           // 5
            UIImage(named: "rollerImage")!,         // 6
            UIImage(named: "shirtImage")!,          // 7
            UIImage(named: "shoeImage")!,           // 8
            UIImage(named: "travelImage")!,         // 9
            UIImage(named: "iconRoundedImage")!,
]

ViewDidLoad:

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeLeft)

respondToGesture

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) 

    if let swipeGesture = gesture as? UISwipeGestureRecognizer 

        switch swipeGesture.direction 
        case UISwipeGestureRecognizer.Direction.left:
            if currentImage == images.count - 1 
                currentImage = 0

            else
                currentImage += 1
            
            swipeImageView.image = images[currentImage]

        case UISwipeGestureRecognizer.Direction.right:
            if currentImage == 0 
                currentImage = images.count - 1
            else
                currentImage -= 1
            
            swipeImageView.image = images[currentImage]
        default:
            break
        
    

我从this SO-post 收到了这个code。我做错什么了吗?我不知道我做错了什么......

【问题讨论】:

你有 swipeImageView 的 isUserInteractionEnabled 吗? @HalR 之前没有,但我更改了它并没有解决问题。另一件事是,一开始根本没有显示图像。 【参考方案1】:

点击图像视图,然后转到属性检查器并确保您已启用启用用户交互

【讨论】:

以上是关于视图未检测到滑动图像库的手势识别器的主要内容,如果未能解决你的问题,请参考以下文章

检测在手势识别器对象区域之外开始的滑动

将向上滑动手势识别器添加到模态视图

从一个视图控制器访问一组图像到另一个视图控制器,以添加滑动手势识别器

将滑动手势识别器添加到 DetailViewContoller

响应一个手势识别器的多个可见视图

使用滑动手势识别器从一个视图控制器滑动到另一个