Disable UIPageViewController bounce

前端 未结 13 2434
离开以前
离开以前 2020-11-27 04:08

Searched a lot for this one, but couldn\'t find a proper solution yet.

Is it possible to disable the bounce effect of a UIPageViewController and still

13条回答
  •  感动是毒
    2020-11-27 04:51

    My solution in Swift 5
    In my scenario, I first load the UIPageViewController on the second page. And I have a total of three pages so I open on the middle one.

    Here's the code of my UIPageViewController

    import UIKit
    
    class MainPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate {
    
      let idList = ["OverviewController", "ImportantItemsController", "ListMenuController"] // A list of all of my viewControllers' storyboard id
      var currentPage = 1 // Tracking the current page
    
      override func viewDidLoad() {
        super.viewDidLoad()
        setupPageController()
    
        for subview in self.view.subviews { // Getting the scrollView
          if let scrollView = subview as? UIScrollView {
            scrollView.delegate = self
            break;
          }
        }
      }
    
      // UIPageViewControllerDataSource
      func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
        if (index > 0) {
          return storyboard?.instantiateViewController(withIdentifier: idList[index - 1])
        }
        return nil
      }
    
      func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
        if (index < idList.count - 1) {
          return storyboard?.instantiateViewController(withIdentifier: idList[index + 1])
        }
        return nil
      }
    
      func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return idList.count
      }
    
      // UIPageViewControllerDelegate
      func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
          guard let vc = pageViewController.viewControllers?.first else { return }
          switch vc {
          case is ImportantItemsController:
              currentPage = 1
          case is OverviewController:
              currentPage = 0
          default:
              currentPage = 2
          }
        }
      }
    
      // ScrollViewDelegate
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let totalViewControllersInPageController = idList.count
        if (currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) {
          scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
        } else if (currentPage == totalViewControllersInPageController - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
          scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
        }
      }
    
      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
        let totalViewControllersInPageController = idList.count
        if (currentPage == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) {
          targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
        } else if (currentPage == totalViewControllersInPageController - 1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
          targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
        }
      }
    
      fileprivate func setupPageController() {
        let controller = storyboard?.instantiateViewController(withIdentifier: idList[1]) as! ImportantItemsController // Loading on the second viewController
        setViewControllers([controller], direction: .forward, animated: true, completion: nil)
        dataSource = self
        delegate = self
      }
    }
    
    

提交回复
热议问题