UIScrollView不正确加载图像(iOS)

时间:2015-05-14 21:49:51

标签: ios swift uiscrollview uiimage

我在iOS8中使用静态表,其中一个单元格中有一个图像滚动条。我遇到一个问题,当加载页面并且图像滚动器不在最初加载的视图上时,图像在视图中未正确加载。

(我没有足够的声誉来发布图片,但可以在这里查看:https://www.dropbox.com/s/qya4rwfe4dd3ryl/Screenshot%202015-05-14%2017.34.38.png?dl=0

但是,在初始视图中加载图像滚动条时,它们的格式正确(https://www.dropbox.com/s/euwmxtvsl2yqbaz/Screenshot%202015-05-14%2017.35.41.png?dl=0

我的代码就是这样:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
    })

}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    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


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

    let pagesScrollViewSize = scrollView.frame
    scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageImages.count), pagesScrollViewSize.height)

    self.loadVisiblePages()


}

loadVisiblePages()方法加载当前应该在滚动视图中的页面时(下面的额外代码) 我必须将tableView.reloadData()放在主队列中,以便标签可以正确调整大小。

会喜欢任何人提供的建议或帮助。 提前致谢

func loadVisiblePages() {

    // First, determine which page is currently visible
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x * 2 + pageWidth) / (pageWidth * 2.0)))


    // Work out which pages you want to load
    let firstPage = max(0,page - 1)
    let lastPage = min(page + 1, pageImages.count - 1)

    // Purge anything before the first page
    for index in 0..<firstPage {
        purgePage(index)
    }

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

    // Purge anything after the last page
    for index in (lastPage + 1)..<(pageImages.count) {
        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
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0.0
        frame = CGRectInset(frame, 10.0, 0.0)

        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
    }
}

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

0 个答案:

没有答案