Tell When a UIPageViewController is Scrolling (for Parallax Scrolling of an Image)

后端 未结 8 1938
灰色年华
灰色年华 2020-12-13 04:43

I am trying to make an effect similar to that found in the new Yahoo weather app. Basically, each page in the UIPageViewController has a background image, and w

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 05:00

    You are not supposed to change the delegate of the page view controller's scroll view: it can break its normal behaviour and/or not be supported later on.

    Instead, you can:

    1. Add a pan gesture to the page view controller's view:

      let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panRecognized(gesture:)))
      view.addGestureRecognizer(panGesture)
      panGesture.delegate = self
      
    2. Add the new function in order to know how the view is being scrolled.

      @objc func panRecognized(gesture: UIPanGestureRecognizer) {
          // Do whatever you need with the gesture.translation(in: view)
      }
      
    3. Declare your ViewController as UIGestureRecognizerDelegate.

    4. Implement this function:

      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
          return true
      }
      

提交回复
热议问题