I follow the @NikNarmo solution, I will write a small quick xcode project to demonstrate the scaling and swap functions.
We hope to help everyone who wants to accomplish the same task.
Some codes from UIScrollView Tutorial: Getting started http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift ,
and some of A Beginners' Guide for UIScrollView http://www.appcoda.com/uiscrollview-introduction/ .
Using Xcode 7.0 and Swift 2.0
override func viewDidLoad() { super.viewDidLoad() mainScrollView = UIScrollView(frame: self.view.bounds) mainScrollView.pagingEnabled = true mainScrollView.showsHorizontalScrollIndicator = false mainScrollView.showsVerticalScrollIndicator = false pageScrollViews = [UIScrollView?](count: photos.count, repeatedValue: nil) let innerScrollFrame = mainScrollView.bounds mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height) mainScrollView.backgroundColor = UIColor.redColor() mainScrollView.delegate = self self.view.addSubview(mainScrollView) configScrollView() addPageControlOnScrollView() }
and the magic is in func scrollViewWillEndDragging when contentSize is equal to mainScrollViewContentSize or not, if it is mainScrollViewController, then swap, otherwise do nothing.
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { let targetOffset = targetContentOffset.memory.x let zoomRatio = scrollView.contentSize.height / mainScrollViewContentSize.height if zoomRatio == 1 { // mainScrollViewController let mainScrollViewWidthPerPage = mainScrollViewContentSize.width / CGFloat(pageControl.numberOfPages) let currentPage = targetOffset / (mainScrollViewWidthPerPage * zoomRatio) pageControl.currentPage = Int(currentPage) loadVisiblePages() } else { // pageScorllViewController } }
And here is the project code https://github.com/Charles-Hsu/ScrollViewDemo
charles.cc.hsu
source share