从另一个视图中使用特定索引的图像启动UIScrollView

时间:2015-05-25 14:31:05

标签: ios swift uiscrollview

我有一个包含多个图像的collectionView。当点击其中一个时,正在打开一个新视图 - UIScrollView。

现在每次UIScrollView都以collectionView的第一个数组图像开始。

我传递了点击到UIScrollView的图像的索引(imageNumber),但我不知道如何使第一张图像显示为带索引的图像。

override func viewDidLoad() {
    super.viewDidLoad()

    var numberOfImagesPerALbum = data[albumNumber].count - 1

    for index in 0...numberOfImagesPerALbum {

        pageImages.append(UIImage(named: data[albumNumber][index]["image"]!)!)


    }

    var pageCount = pageImages.count


    // 2
    pageControl.currentPage = imageNumber
    println(imageNumber)

    pageControl.numberOfPages = pageCount

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


}

override func viewDidLayoutSubviews() {
    let pagesScrollViewSize = scrollView.frame.size
    scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageImages.count), pagesScrollViewSize.height)

    // 5
    loadVisiblePages()

}


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
    }

    // 1
    if let pageView = pageViews[page] {
        // Do nothing. The view is already loaded.

    } else {
        // 2
        var frame = scrollView.bounds
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0.0

         3
        let newPageView = UIImageView(image: pageImages[page])

        newPageView.contentMode = .ScaleAspectFit
        newPageView.frame = frame


        scrollView.addSubview(newPageView)

        // 4
        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 loadVisiblePages() {
    // First, determine which page is currently visible
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
    println("Page is \(page)")

    imageTitleLabel.text = data[albumNumber][page]["title"]

    imageDescriptionLabel.text = data[albumNumber][page]["text"]

    // 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 var index = firstPage; index <= lastPage; ++index {
        loadPage(index)
    }

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

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

1 个答案:

答案 0 :(得分:1)

考虑将UICollectionViewUICollectionViewFlowLayout一起使用(将scrollDirection设置为UICollectionViewScrollDirectionHorizontal)而不是UIScrollView。同时将UICollectionView的{​​{1}}属性设置为pagingEnabled

这将为您节省大量不必要的代码,您将免费获得true的{​​{1}}方法“。这应该可以解决您的所有问题并使代码更清晰。祝你好运!