如何使用UIPageViewController在第一个和最后一个索引处停止UIScrollView的反弹?

时间:2017-02-01 13:51:59

标签: ios swift uiscrollview uipageviewcontroller bounce

我有一个UIViewController,我通过编程方式添加了UIPageViewController。如果我们处于0索引或最后一个索引,如何停止反弹(滚动)UIViewController?我的意思是反弹。

您可以查看以下链接:How to create PageView with UITabBar or Segment Control using swift?

这是我的代码:

import UIKit

class ViewController: UIViewController {

fileprivate var pageViewController:UIPageViewController!
@IBOutlet weak var paginationView:UIView!
@IBOutlet var btnYourGroup: UIButton!
@IBOutlet var btnSharedBy: UIButton!
@IBOutlet var sliderViewLeadingConst: NSLayoutConstraint!

fileprivate var selectedIndex : Int? {
    didSet {
        var leading:CGFloat = 0
        if selectedIndex == 0 {
            leading = 0
        }
        else {
            leading = self.view.frame.size.width/2
        }

        self.sliderViewLeadingConst.constant = leading

        UIView.animate(withDuration: 0.2, animations: {

            self.view.layoutIfNeeded()
        })
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    //SetUp Pagination
    setupPagination()

    for view in self.pageViewController.view.subviews {
        if let scrollView = view as? UIScrollView {
            scrollView.delegate = self
            break
        }
    }

    //btnSharedBy.titleColor(for: UIControlState)
    btnSharedBy.setTitleColor(UIColor.lightGray, for: UIControlState())
}

private func setupPagination() {

    let startVC = self.viewControllerAtIndex(index: 0)
    let vc = [startVC]
    self.pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    self.pageViewController.setViewControllers(vc, direction: .reverse, animated: true, completion: nil)
    self.pageViewController.dataSource = self
    self.pageViewController.delegate = self
    self.pageViewController.view.frame.size = self.paginationView.frame.size
    self.paginationView.addSubview(self.pageViewController.view)
}

@IBAction func mainCateogryTapped(sender:UIButton) {
    loadNextController(index:sender.tag)
}

fileprivate func viewControllerAtIndex(index:Int) -> PageContentViewController {

    let feedsVC = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
    feedsVC.pageIndex = index
    return feedsVC
}

fileprivate func loadNextController(index:Int) {

    if selectedIndex == index {
        return
    }

    let nextViewController = viewControllerAtIndex(index:index)
    selectedIndex = index

    if index == 0 {
        btnSharedBy.setTitleColor(UIColor.lightGray, for: UIControlState())
        btnYourGroup.setTitleColor(UIColor.white, for: UIControlState())
        self.pageViewController?.setViewControllers([nextViewController], direction: UIPageViewControllerNavigationDirection.reverse, animated: true, completion: nil)
    } else if index == 1 {
        btnSharedBy.setTitleColor(UIColor.white, for: UIControlState())
        btnYourGroup.setTitleColor(UIColor.lightGray, for: UIControlState())
        self.pageViewController?.setViewControllers([nextViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
    }

    else {

    }
  }
}
extension ViewController: UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    var index = (viewController as! PageContentViewController).pageIndex

    if index == 0 {
        return nil
    }
    index = index - 1
    return viewControllerAtIndex(index:index)

}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    var index = (viewController as! PageContentViewController).pageIndex

    index = index + 1
    if index == 2 {
        //self.pageViewController.
        return nil
    }
    return viewControllerAtIndex(index:index)
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {

    let vc : PageContentViewController = pendingViewControllers[0] as! PageContentViewController
    selectedIndex = vc.pageIndex
  }
}

extension ViewController : UIPageViewControllerDelegate {

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed {
        let vc = previousViewControllers.first as! PageContentViewController

        if 0 == vc.pageIndex {
            selectedIndex = 1
            btnSharedBy.setTitleColor(UIColor.white, for: UIControlState())
            btnYourGroup.setTitleColor(UIColor.lightGray, for: UIControlState())

        }

        else {
            btnYourGroup.setTitleColor(UIColor.white, for: UIControlState())
            btnSharedBy.setTitleColor(UIColor.lightGray, for: UIControlState())
            selectedIndex = 0
        }
    }

    else if !completed {
        let pageContentView = previousViewControllers[0] as! PageContentViewController;
        self.selectedIndex = pageContentView.pageIndex
    }
  }
}

//ScrollView Delegate
extension ViewController : UIScrollViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let point = scrollView.contentOffset
    var percentComplete: CGFloat
    percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
    //NSLog("percentComplete: %f", percentComplete)

        if(0.8 > percentComplete)
        {
            //selectedIndex = 1
            scrollView.bounces = false
        }

        else
        {
            scrollView.bounces = true

        }
    }
}

如何使用按钮指示器在第一个和最后一个索引处停止弹跳问题?

1 个答案:

答案 0 :(得分:2)

您可以浏览页面视图控制器的子视图,查找它的滚动视图,并将跳出设置为false。然而,这是一个脆弱的解决方案,并且不明智。 (弄乱视图控制器的视图通常是一个坏主意,特别是如果没有公共界面可以找到它们。)

下面的线程有代码,用于遍历页面视图控制器的子视图并查找滚动视图。可以很容易地调整该代码以将bounces属性设置为false:

Progress of UIPageViewController