在分页UIScrollView中缩放图像

时间:2015-12-24 08:03:25

标签: ios swift uiscrollview zooming

我正在this tutorial之后创建一个分页UIScrollView

这是允许使用UIScrollView分页图像的Swift代码。但是,它有一个限制。我无法缩放图像。

class PagedScrollViewController: UIViewController, UIScrollViewDelegate {

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

var pageImages: [UIImage] = []
var pageViews: [UIImageView?] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // 1
    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

    // 2
    pageControl.currentPage = 0
    pageControl.numberOfPages = pageCount

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

    // 4
    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)))

    // 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()
}

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


}

如何使图像可缩放?

2 个答案:

答案 0 :(得分:2)

使用来自的代码解决了问题 https://github.com/Charles-Hsu/ScrollViewDemo

答案 1 :(得分:1)

您可以创建一个源自UIScrollView的自定义类,其名称为ImageScrollView,其中有UIImageView
使用 Interface Builder 创建此项。现在,在页面视图数组中,您需要添加ImageScrollView而不是UIImageView,您将获得缩放效果。