uipageviewcontroller 禁用向右滑动

问题描述 投票:0回答:3

我有一个 uipageviewcontroller,里面有四个单独的视图控制器。我想禁用第一个视图控制器的右滑动操作并禁用第四个视图控制器的左滑动操作。当应用程序启动时,我希望用户只能向左滑动而不能向右滑动,因为向右滑动会将用户带到第四个视图控制器,而在第四个视图控制器上向左滑动会将用户带到第一个视图控制器。这是我的代码。

class TutorialPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    weak var tutorialDelegate: TutorialPageViewControllerDelegate?

    lazy var VCArr: [UIViewController] = {
        return [self.VCInstance(name: "FirstVC"),
                self.VCInstance(name: "SecondVC"),
                self.VCInstance(name: "ThirdVC"),
                self.VCInstance(name: "FourthVC")]

    }()

    private func VCInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let pageControl = UIPageControl.appearance()
        //Customizing
        pageControl.pageIndicatorTintColor = UIColor.lightGray
        pageControl.currentPageIndicatorTintColor = UIColor(red:0.23, green:0.73, blue:1.00, alpha:1.0)

        dataSource = self
        delegate = self
        self.dataSource = self
        self.delegate = self

        if let FirstVC = VCArr.first {
            setViewControllers([FirstVC], direction: .forward, animated: true, completion: nil)
            tutorialDelegate?.tutorialPageViewController(tutorialPageViewController: self, didUpdatePageCount: VCArr.count)

        }
    }

   override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        for view in self.view.subviews {
            if view is UIScrollView {
                view.frame = UIScreen.main.bounds
            } else if view is UIPageControl {
                view.backgroundColor = UIColor.clear
            }
        }
    }

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCArr.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return VCArr.last
        }

        guard VCArr.count > previousIndex else {
            return nil
        }
        return VCArr[previousIndex]
    }

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCArr.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1

        guard nextIndex < VCArr.count else {
            return VCArr.first
        }

        guard VCArr.count > nextIndex else {
            return nil
        }
        return VCArr[nextIndex]
    }

    public func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return VCArr.count
    }


    public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
        let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
             return 0
         }
         return firstViewControllerIndex
     }
}

extension TutorialPageViewController {

    func pageViewController(pageViewController: UIPageViewController,
                            didFinishAnimating finished: Bool,
                            previousViewControllers: [UIViewController],
                            transitionCompleted completed: Bool) {
        if let firstViewController = viewControllers?.first,
            let index = VCArr.index(of: firstViewController) {
            tutorialDelegate?.tutorialPageViewController(tutorialPageViewController: self, didUpdatePageIndex: index)
        }
    }

}
ios swift swipe uipageviewcontroller
3个回答
2
投票

基本上你需要更改以下几行

guard previousIndex >= 0 else {
            return nil
        }


guard nextIndex < VCArr.count else {
            return nil
        }

----- 或 -----

将 viewControllerAfter 更改为

 public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            guard let viewControllerIndex = VCArr.index(of: viewController) else {
                return nil
            }

            let nextIndex = viewControllerIndex + 1

            guard nextIndex < VCArr.count else {
            return nil
        }

            guard VCArr.count > nextIndex else {
                return nil
            }

            return VCArr[nextIndex]
        }

将 ViewControllerBefore 更改为

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCArr.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

       guard previousIndex >= 0 else {
            return nil
        }
        guard VCArr.count > previousIndex else {
            return nil
        }
        return VCArr[previousIndex]
    }

1
投票

您可以使用此扩展来禁用所有方向滑动

extension UIPageViewController {
     var isPagingEnabled: Bool {
        get {
           var isEnabled: Bool = true
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   isEnabled = subView.isScrollEnabled
               }
           }
           return isEnabled
       }
       set {
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   subView.isScrollEnabled = newValue
               }
           }
       }
   }    
}

并称之为:

pageCtrl.isPagingEnabled = false

0
投票
  for view in self.view.subviews  {
    if let view = view as? UIScrollView {
      view.bounces = true
    }
  }

这段代码确实帮助我隐藏了空格,所以我们无法在最后一个屏幕(最左边)之后或第一个屏幕(最右边)之前滑动。

.isScrollEnabled = false

这段代码并没有真正的帮助,因为它完全阻止了页面之间的滑动。

© www.soinside.com 2019 - 2024. All rights reserved.