禁用用户仅在UIPageViewController上以特定方向刷卡?

时间:2018-10-04 20:42:41

标签: ios swift uitableview uipageviewcontroller

我在UIPageViewController中嵌入的UITableviewController遇到了麻烦……系统无法区分向左滑动以更改页面和向滑动滑动以删除UITableView中的项目。这个特定的UITableViewController恰好在PageViewController的最后一页上,因此,由于向左滑动并没有真正做任何事情(向右滑动),我可以禁止PageViewController在当前页面== 3时检测滑动吗?棘手的部分是我只想要忽略向左滑动(以便tableView将左滑动读取为要删除的滑动),而我仍想检测向右滑动,以便用户可以导航回第2页。

UIPageViewController代码:

class WorkoutPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {


    //var pageControl = UIPageControl()
    var referenceToNewWorkoutKingVC: NewWorkoutKingViewController?


    // MARK: UIPageViewControllerDataSource

    lazy var orderedViewControllers: [UIViewController] = {

        var viewControllers = [UIViewController]()

        if referenceToNewWorkoutKingVC?.sessionType == goalieSessionString || referenceToNewWorkoutKingVC?.sessionType == refSessionString || referenceToNewWorkoutKingVC?.sessionType == coachSessionString || referenceToNewWorkoutKingVC?.sessionType == openSkateSessionString {

            viewControllers = [self.newVc(viewController: "NewIceDataTableViewController"),
                               self.newVc(viewController: "NewHealthDataTableViewController")]

        } else {
            viewControllers = [self.newVc(viewController: "NewIceDataTableViewController"),
                               self.newVc(viewController: "NewHealthDataTableViewController"),
                               self.newVc(viewController: "NewShiftTableViewController")]

        }


       return viewControllers
    }()

     let impact = UIImpactFeedbackGenerator(style: UIImpactFeedbackGenerator.FeedbackStyle.medium)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self


        // This sets up the first view that will show up on our page control
        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }

    }



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




    // MARK: Delegate methods
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        let pageContentViewController = pageViewController.viewControllers![0]
        referenceToNewWorkoutKingVC?.pageControl.numberOfPages = orderedViewControllers.count
        referenceToNewWorkoutKingVC?.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!


    }

    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        //Haptic when pages switch, code from: https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator
        impact.prepare()
        impact.impactOccurred()
    }

    // MARK: Data source functions.
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        // User is on the first view controller and swiped left to loop to
        // the last view controller.
        guard previousIndex >= 0 else {
            //return orderedViewControllers.last
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            return nil
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }

        return orderedViewControllers[previousIndex]
    }

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

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count

        // User is on the last view controller and swiped right to loop to
        // the first view controller.
        guard orderedViewControllersCount != nextIndex else {
            //return orderedViewControllers.first
            // Uncomment the line below, remove the line above if you don't want the page control to loop.
            return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }

}

enter image description here

1 个答案:

答案 0 :(得分:0)

  

尝试一下

func manegePageControllerBounces(isEnable : Bool) {

    for view in pageViewController.view.subviews {

        if view is UIScrollView {
            let scrollView = view as? UIScrollView
            scrollView?.bounces = isEnable
        }
    }

}

  

根据要求切换弹跳

self.manegePageControllerBounces(isEnable:false)

希望对您有帮助。...