简体   繁体   English

uicollectionView在Swift iOS视图之间滚动?

[英]UicollectionView scroll between views in swift ios?

My collection view is working great. 我的收藏夹视图效果很好。 It shows a grid of photos and lists hundreds of them. 它显示照片网格并列出数百张照片。 You can swipe vertically to scroll through them all. 您可以垂直滑动以滚动浏览所有内容。 Life is good. 生活很好。 However, I now have a new requirement. 但是,我现在有一个新的要求。 I need to be able to detect when the user is swiping left or right. 我需要能够检测用户何时向左或向右滑动。 I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. 我需要能够拦截此手势,以便可以在保持集合视图的垂直滚动功能不变的情况下将行为附加到左右滑动。 Any ideas? 有任何想法吗?

In swift? 迅速?

If it helps for reference heres a link to my Github project. 如果可以帮助参考,请访问我的Github项目链接。

https://github.com/StarShowsStudios/GodCards https://github.com/StarShowsStudios/GodCards

If you open the project in Xcode you can see the detail view controller. 如果在Xcode中打开项目,则可以看到详细信息视图控制器。 It gets its information from a plist file called cards according to the selected collection cell controller 它根据选定的收集单元控制器从称为卡的plist文件中获取其信息。

You can add left and right swipe gesture recognizer to detect left and right swipe. 您可以添加左右滑动手势识别器来检测左右滑动。

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
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM