Zoom Images in Paged UIScrollView

北战南征 提交于 2020-01-06 03:28:29

问题


I am creating a paging UIScrollView following this tutorial.

This is the Swift code which allow paging of images with UIScrollView. However, it has a limitation. I am unable to zoom the images.

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


}

How to make the images zoomable?


回答1:


Problem solved using the code from https://github.com/Charles-Hsu/ScrollViewDemo




回答2:


You can create a custom class derived from UIScrollView named as ImageScrollView in which there is a UIImageView.
Create this using Interface Builder. Now in page views array you need to add ImageScrollView instead of UIImageView and you will achieve the zooming effect.



来源:https://stackoverflow.com/questions/34449359/zoom-images-in-paged-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!