使用 UIScrollView 进行分页

Paging using UIScrollView

我正在尝试使用 UIScrollView 和页面控件显示多个图像,想想 App Store 上应用程序的屏幕截图。但出于某种原因,我的 UIScrollView 没有滚动。我检查了一下,UISCrollView's contentSize 的宽度大于 UIScrollView's 的宽度。可能还值得注意的是,我将页面控件放在 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()
}
}

可能是什么导致了这个问题?

最好使用 UIPageViewController 你只想显示图像。 你可以在那里看到教程there