简体   繁体   中英

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.

https://github.com/StarShowsStudios/GodCards

If you open the project in Xcode you can see the detail view controller. It gets its information from a plist file called cards according to the selected collection cell controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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