Esqarrouth / EZSwipeController

:point_up_2: UIPageViewController like Snapchat/Tinder/iOS Main Pages
MIT License
842 stars 71 forks source link

disable scroll when reaches the most left or right view controller #44

Open ghost opened 8 years ago

ghost commented 8 years ago

It will be nicer if we can disable the scroll to make it more like snapchat style.

Esqarrouth commented 8 years ago

Its more like tinder style atm

ghost commented 8 years ago

Thanks for the great project, I have used EZSwipeController successfully in my own project, which has three viewcontrollers and the left most one is for message and the right most one is for system setting, so it's important for me to disable scrolling when it's in message or setting sections, because otherwise it's quite ugly. I successfully achieved this by implementing the delegate of UIScrollViewDelegate as

extension MySwipeVC: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if self.currentVCIndex == 0 && (scrollView.contentOffset.x < scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if self.currentVCIndex == self.stackVC.count - 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<CGPoint>) {
        if self.currentVCIndex == 0 && (scrollView.contentOffset.x < scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if self.currentVCIndex == self.stackVC.count - 1 && (scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
    }
}

And in viewWillAppear, I added

  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        for someview in self.pageViewController.view.subviews {
            if someview is UIScrollView {
                let scrollview = someview as! UIScrollView
                scrollview.delegate = self
            }
        }
    }

To make this actually work, in EZSwipeController, it should update currentStackVC after pageViewController completed setViewControllers, I have made a PR in case other people need the same feature as I.

HackShitUp commented 7 years ago

@liyukuang did you by any chance initialize a UITableViewController in the EZSwipeController? I opened a similar issue because I have an array of objects (n) that sorts out which UITableViewController to show. But is it even possible to initialize multiple, and unknown numbers of UITableViewControllers in this???