2014-11-01 75 views
2

希望您可以在此給我一些指導。我有一個垂直分頁的滾動視圖設置。我的問題是視圖大於屏幕(垂直)。我希望的效果是讓視圖滾動到底部,然後翻到下一頁。就像我下面的圖片試圖描繪的一樣。在垂直分頁啓用的情況下在UIScrollView中垂直滾動

我已經嘗試將scrollview的大小和內容大小設置爲正確地偏移視圖的視圖的大小。我只是無法滾動查看視圖的底部,它只是翻到下一個視圖。

感謝您的任何意見。

Layout Design

class ViewController: UIViewController { 

let scrollView = UIScrollView() // Create the scrollView 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    //Set up and add scrollView to view 
    scrollView.frame = self.view.frame 
    self.scrollView.pagingEnabled = true 
    self.view.addSubview(scrollView) 

    //An array of UIColors to add to the views 
    let x : [UIColor] = [UIColor.blueColor(),UIColor.redColor(),UIColor.yellowColor()] 

    //For each UIColor add a view that is 100px larger then the height of the scrollView 
    for index in 0...x.count-1{ 
     // 
     let subView = UIView(frame: CGRectMake(
      0, //x offset 
      (self.scrollView.frame.height + 100) * CGFloat(index), //y offset 
      self.scrollView.frame.width, // width 
      (self.scrollView.frame.height + 100))) // height 
     subView.backgroundColor = x[index] //background Color 
     scrollView.addSubview(subView) // Add View 

    } 

    // 
    let c = (self.scrollView.frame.size.height + 100) * CGFloat(x.count) 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width, c) 

    //Background Color 
    self.view.backgroundColor = UIColor.greenColor() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 
+0

你的意思是垂直的,對吧? – rdelmar 2014-11-01 03:59:57

+0

* Facepalm *。是的我的意思是垂直。固定 – User4 2014-11-01 04:01:06

+0

大小總是需要保持屏幕大小,並且您需要根據圖像數量設置contentSize,如果幀和內容都相同,那麼它將如何滾動。 – Balu 2014-11-01 05:18:07

回答

3

我發現這樣做是使用嵌套的滾動視圖的內容的最好方式。這是我的代碼最終看起來像。

class ViewController: UIViewController, UIScrollViewDelegate { 

let scrollView = ScrollView() // Create the scrollView 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    //Set up and add scrollView to view 
    scrollView.frame = self.view.frame 
    self.scrollView.pagingEnabled = true 
    self.view.addSubview(scrollView) 
    self.scrollView.delegate = self 

    //An array of UIColors to add to the views 
    let x : [UIColor] = [UIColor.blueColor(),UIColor.redColor(),UIColor.yellowColor()] 

    //For each UIColor add a view that is 100px larger then the height of the scrollView 
    for index in 0...x.count-1{ 
     // 
     let subView = UIScrollView(frame: CGRectMake(
      0, //x offset 
      (self.scrollView.frame.height * CGFloat(index)), //y offset 
      self.scrollView.frame.width, // width 
      (self.scrollView.frame.height))) // height 

//Set the size of the content view 
     let contentView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 1000)) 

     subView.contentSize = CGSizeMake(self.view.frame.width, contentView.frame.height) 
     contentView.backgroundColor = x[index] 
     subView.addSubview(contentView) 
     scrollView.addSubview(subView) // Add View 

    } 

    // 
    let c = (self.scrollView.frame.size.height) * CGFloat(x.count) 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width, c) 

    //Background Color 
    self.view.backgroundColor = UIColor.greenColor() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 
2

用戶4的回答,在Objective-C:

UIScrollView *scrollView = [[UIScrollView alloc] init]; 
scrollView.frame = self.view.frame; 
scrollView.pagingEnabled = YES; 
scrollView.backgroundColor = [UIColor yellowColor]; 
[self.view addSubview:scrollView]; 
scrollView.delegate = self; 
NSArray *x = @[[UIColor blueColor], [UIColor redColor]]; 
for (int i = 0; i < x.count; i++) { 
    UIView *subView = [[UIView alloc] initWithFrame: 
         CGRectMake(0, 
            (scrollView.frame.size.height) * i, 
            scrollView.frame.size.width, 
            scrollView.frame.size.height) 
         ]; 
    UISwitch *switchCtrl = [[UISwitch alloc] initWithFrame:CGRectMake(5, 5, 20, 20)]; 
    subView.backgroundColor = x[i]; 
    [subView addSubview:switchCtrl]; 
    [scrollView addSubview:subView]; 
} 
float c = (scrollView.frame.size.height) * x.count; 
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, c); 
self.view.backgroundColor = [UIColor greenColor]; 
0

一個偉大的教程由RayWenderlich.com確實水平滾動頁面。我修改了代碼,通過添加一個名爲「horizo​​ntalDirection」的變量來允許垂直頁面滾動。首先跟隨他的教程在這裏: http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

你已經完成了「查看上一頁/下一頁頁」的部分之後這個替換您的視圖控制器:(您可能需要調整您的滾動型的大小)

@IBOutlet var scrollView: UIScrollView! 
@IBOutlet var pageControl: UIPageControl! 

var pageImages: [UIImage] = [] 
var pageViews: [UIImageView?] = [] 
let horizontalDirection = false 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Set up the image you want to scroll & zoom and add it to the scroll view 
    pageImages = [UIImage(named: "photo1.png")!, 
     UIImage(named: "photo2.png")!, 
     UIImage(named: "photo3.png")!, 
     UIImage(named: "photo4.png")!, 
     UIImage(named: "photo5.png")!] 

    let pageCount = pageImages.count 

    // Set up the page control 
    pageControl.currentPage = 0 
    pageControl.numberOfPages = pageCount 

    // Set up the array to hold the views for each page 
    for _ in 0..<pageCount { 
     pageViews.append(nil) 
    } 

    // Set up the content size of the scroll view 
    let pagesScrollViewSize = scrollView.frame.size 
    if horizontalDirection { 
     scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageImages.count), pagesScrollViewSize.height) 
    } else { 
     scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width, CGFloat(pageImages.count) * pagesScrollViewSize.height) 
    } 
    // Load the initial set of pages that are on screen 
    loadVisiblePages() 
} 
func loadVisiblePages() { 
    // First, determine which page is currently visible 
    var pageSide = scrollView.frame.size.width 
    var page = Int(floor((scrollView.contentOffset.x * 2.0 + pageSide)/(pageSide * 2.0))) 
    if !horizontalDirection { 
     pageSide = scrollView.frame.size.height 
     page = Int(floor((scrollView.contentOffset.y * 2.0 + pageSide)/(pageSide * 2.0))) 
    } 

    // Update the page control 
    pageControl.currentPage = page 

    // Work out which pages you want to load 
    let firstPage = page - 1 
    let lastPage = page + 1 

    // Purge anything before the first page 
    for var index = 0; index < firstPage; ++index { 
     purgePage(index) 
    } 

    // Load pages in our range 
    for index in firstPage...lastPage { 
     loadPage(index) 
    } 

    // Purge anything after the last page 
    for var index = lastPage+1; index < pageImages.count; ++index { 
     purgePage(index) 
    } 
} 

func loadPage(page: Int) { 
    if page < 0 || page >= pageImages.count { 
     // If it's outside the range of what you have to display, then do nothing 
     return 
    } 

    // Load an individual page, first checking if you've already loaded it 
    if let pageView = pageViews[page] { 
     // Do nothing. The view is already loaded. 
    } else { 
     var frame = scrollView.bounds 
     if horizontalDirection { 
      frame.origin.x = frame.size.width * CGFloat(page) 
      frame.origin.y = 0.0 
      frame = CGRectInset(frame, 10.0, 0.0) 
     } else { 
      frame.origin.x = 0.0 
      frame.origin.y = frame.size.height * CGFloat(page) 
     } 

     let newPageView = UIImageView(image: pageImages[page]) 
     newPageView.contentMode = .ScaleAspectFit 
     newPageView.frame = frame 
     scrollView.addSubview(newPageView) 
     pageViews[page] = newPageView 
    } 
} 

func purgePage(page: Int) { 
    if page < 0 || page >= pageImages.count { 
     // If it's outside the range of what you have to display, then do nothing 
     return 
    } 

    // Remove a page from the scroll view and reset the container array 
    if let pageView = pageViews[page] { 
     pageView.removeFromSuperview() 
     pageViews[page] = nil 
    } 
} 

func scrollViewDidScroll(scrollView: UIScrollView!) { 
    // Load the pages that are now on screen 
    loadVisiblePages() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
}