senty
senty

Reputation: 12847

Vertical Paging Using UIScrollView

I have a UIScrollView and inside it a UIView and a UITableView. I want to do paging so in the first scroll down, the view swipes down and position TableView to the top (like vertical paging). So when user comes to page, he sees UIView and once swiped down, it scrolls until TableView is positioned at the top of the page, and then classic TableView. Then, if the user swipes up further than the top of TableView, then vertical paging back to UIView.

UIScrollView
   |------------------|
   | |--------------| |
   | |    UIView    | |
   | |              | |
   | |              | |
   | |              | |
   | |              | |
   | |              | |
   | |--------------| |
   | |   TableView  | |
   | |              | |
   | |     ...      | |
   | |--------------| |
   |------------------|

Before, I used scrollViewWillEndDragging for getting swipe up & down gestures. But I am confused how to achieve vertical paging.

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

   if targetContentOffset.memory.y < scrollView.contentOffset.y {

      // Going Up
       if Page != 0 {  // should also check if TableView is at top.
           // Scroll Up To UIView
       }
    } else {
       if 0 < scrollOffset {

            if Page == 0 { 
                // Scroll Down to the top of TableView    

                // I can also check if view is at the top maybe 200 pixels, but      
                // I think this would also work.

            }
       }
    }       
}

Upvotes: 1

Views: 2263

Answers (1)

Oleg Sherman
Oleg Sherman

Reputation: 2802

  1. UIScrollView has pagingEnabled property, just set it to true, but make sure you set content size correctly.

    let scrollView = UIScrollView(frame: view.bounds)
    scrollView.pagingEnabled = true
    view.addSubview(scrollView)
    
    let pageSize = view.bounds.size
    
    let upperView = UIView()
    upperView.backgroundColor = UIColor.redColor()
    
    let lowerView = UITableView()
    lowerView.backgroundColor = UIColor.blueColor()
    
    let pagesViews = [upperView , lowerView]
    
    let numberOfPages = pagesViews.count
    
    for (pageIndex,page) in pagesViews.enumerate(){
        page.frame = CGRect(origin: CGPoint(x:0 , y:CGFloat(pageIndex) * pageSize.height), size: pageSize)
        scrollView.addSubview(page)
    }
    
    scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))
    
  2. Use UIPageViewController with vertical scroll direction, but you need to use a UIViewController for a page.

Upvotes: 3

Related Questions