2015-07-11 95 views
0

我試圖使用UIScrollView和一個頁面控件顯示多個圖像,請考慮App Store上的應用程序截圖。但由於某種原因,我UIScrollView不滾動。我檢查了,UISCrollView's contentSize的寬度大於UIScrollView's的寬度。也可能值得注意的是,我將頁面控件放在UIScrollView中,以便它顯示在圖片的頂部。我的代碼如下:使用UIScrollView進行分頁

import UIKit 

class ItemDetailViewController: UIViewController, UIScrollViewDelegate { 

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


var itemSelected: Item! 
var pageViews: [UIImageView?] = [] 
var pageCount: Int! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 

    pageCount = itemSelected.images.count 
    pageControl.currentPage = 0 
    pageControl.numberOfPages = pageCount 

    for _ in 0..<pageCount { 
     pageViews.append(nil) 
    } 

    scrollView.frame.size = CGSizeMake(view.frame.width, view.frame.height/2.0) 
    let pageSize = scrollView.frame.size 
    scrollView.contentSize = CGSizeMake(pageSize.width * CGFloat(pageCount), pageSize.height) 
    loadVisiblePages() 
    scrollView.delegate = self 
} 

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

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    tabBarController?.tabBar.hidden = true 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewDidDisappear(animated) 
    tabBarController?.tabBar.hidden = false 
} 


// MARK: - Helper Functions 

func loadPage(page: Int){ 
    if page < 0 || page >= pageCount { 
     // page outside of range, do nothing 
     return 
    } 

    if let pageView = pageViews[page] { 
     // page already loaded, do nothing 
     return 
    } else { 
     var frame = scrollView.bounds 
     frame.origin.x = frame.size.width * CGFloat(page) 
     frame.origin.y = 0.0 

     let newPageView = UIImageView(image: itemSelected.images[page]) 
     newPageView.contentMode = .ScaleToFill 
     newPageView.frame = frame 
     scrollView.addSubview(newPageView) 

     pageViews[page] = newPageView 
    } 


} 

func purgePage(page: Int){ 
    if page < 0 || page >= pageCount { 
     // page outside of range, do nothing 
     return 
    } 

    if let pageView = pageViews[page]{ 
     pageView.removeFromSuperview() 
     pageViews[page] = nil 
    } 
} 

func loadVisiblePages(){ 
    let pageWidth = scrollView.frame.size.width 
    let page = Int(floor(scrollView.contentOffset.x * 2.0 + pageWidth)/(2.0 * pageWidth)) 

    pageControl.currentPage = page 

    let firstPage = page - 1 
    let lastPage = page + 1 

    for var index = 0; index < firstPage; index++ { 
     purgePage(index) 
    } 

    for index in firstPage ... lastPage { 
     loadPage(index) 
    } 

    for var index = lastPage + 1; index < itemSelected.images.count; index++ { 
     purgePage(index) 
    } 
} 

// MARK: - Scroll View Delegate Methods 

func scrollViewDidScroll(scrollView: UIScrollView) { 
    loadVisiblePages() 
} 
} 

什麼可能導致這個問題?

+0

改爲在viewDidLayoutSubviews中設置滾動視圖的內容大小。 –

+0

感謝您的快速響應,但仍然無法滾動。 – modsoussi

+0

你在哪裏初始化選項?似乎設置pageCount時它是空的。看起來像pageCount = 0. –

回答

1

更好的使用UIPageViewController是你想只顯示圖像。 你可以看到教程如何在那裏there

+0

這實際上是我正在尋找!謝謝! – modsoussi

+0

非常歡迎;) –