简体   繁体   English

如何在页面视图控制器上检测滑动?

[英]How to detect a swipe on a Page View Controller?

即使没有任何视图控制器可以滑动,我如何在页面视图控制器上检测到滑动?

The Page View Controller has a built in gesture recognizer contained within the scrollView. Page View Controller具有一个内置的手势识别器,它包含在scrollView中。 It's a pan gesture recognizer (see https://developer.apple.com/documentation/uikit/uiscrollview/1619425-pangesturerecognizer ). 它是一个平移手势识别器(请参阅https://developer.apple.com/documentation/uikit/uiscrollview/1619425-pangesturerecognizer )。 However, the scroll view property of the Page View Controller is not public (see https://developer.apple.com/documentation/uikit/uipageviewcontroller ). 但是,页面视图控制器的滚动视图属性不是公共的(请参阅https://developer.apple.com/documentation/uikit/uipageviewcontroller )。 You could extend the Page View Controller class to alter this behavior and expose the scroll view in order to get the pan gestures that continue to occur after the user has scrolled through all of the available views. 您可以扩展Page View Controller类以更改此行为,并公开滚动视图,以获取在用户滚动浏览所有可用视图之后继续发生的平移手势。

extension UIPageViewController {

    public var scrollView: UIScrollView? {
        for view in self.view.subviews {
            if let scrollView = view as? UIScrollView {
                return scrollView
            }
        }
        return nil
    }

}

Next you should add your root view controller as a target of the scroll view's pan gesture in your view did load method and include the method you would like to call when the scroll view's gesture recognizer detects a pan gesture. 接下来,您应该在视图执行加载方法中将根视图控制器添加为滚动视图的平移手势的目标,并包括当滚动视图的手势识别器检测到平移手势时要调用的方法。 You can track the number of swipes with a property stored on your root view controller as well. 您也可以使用存储在根视图控制器上的属性来跟踪滑动次数。 Note that if you want to track the swipes beyond the final view controller you'll need to set up a bool property on your root view controller that gets set to true after the user swipes to the final view in the page view controller and only start tracking swipes after this threshold is met. 请注意,如果要跟踪最终视图控制器之外的滑动,则需要在根视图控制器上设置bool属性,该属性在用户在页面视图控制器中滑动到最终视图并仅启动后会设置为true达到此阈值后跟踪刷卡。

override func viewDidLoad() {
   . . .
   pvController.scrollView?.panGestureRecognizer.addTarget(self, action:#selector(self.handlePan(sender:)))
}

func handlePan(sender:UIPanGestureRecognizer) {
    switch sender.state {
    case .ended:
        self.numberOfSwipes = self.numberOfSwipes + 1
        print("User Swiped ")
        print(self.numberOfSwipes)
    default:
        break
    }
}

You probably want to set up a bool property on your view controller that gets triggered after the user consumes all of the views so that you know the swipe is one that occurs beyond the views. 您可能希望在视图控制器上设置一个bool属性,该属性在用户使用完所有视图之后触发,以便您知道滑动是在视图之外发生的。 I placed the code for the app in a public repo at https://bitbucket.org/stonybrooklyn/pageviewcontroller/ for your reference. 我将该应用程序的代码放置在https://bitbucket.org/stonybrooklyn/pageviewcontroller/的公共仓库中,以供您参考。

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

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