UicollectionView在swift ios中的视图之间滚动?

时间:2016-06-18 01:43:02

标签: ios swift

我的收藏视图效果很好。它显示了一个照片网格,并列出了数百个照片。您可以垂直滑动以全部滚动它们。生活很好。但是,我现在有了新的要求。我需要能够检测用户何时向左或向右滑动。我需要能够拦截这个手势,这样我就可以将行为附加到左右滑动,同时保持我的集合视图的垂直滚动功能。有什么想法吗?

在swift?

如果它有助于参考继承我的Github项目的链接。

https://github.com/StarShowsStudios/GodCards

如果在Xcode中打开项目,则可以看到详细视图控制器。它根据所选的集合单元控制器

从称为卡的plist文件中获取其信息

1 个答案:

答案 0 :(得分:0)

您可以添加左右滑动手势识别器来检测左右滑动。

override func awakeFromNib() {
        super.awakeFromNib()

 // Add Left Swipe Gesture
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        self.addGestureRecognizer(swipeLeft)

        // Add Right Swipe Gesture
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.addGestureRecognizer(swipeRight)
     }

    // This function detects Swipe direction and perform action
    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                print("Swiped right")
                rightSwipeAction()

            case UISwipeGestureRecognizerDirection.Left:
                print("Swiped left")
                leftSwipeAction()

            default:
                break
            }
        }
    }