How to Zoom In/Out Photo on double Tap in the iPhone WWDC 2010 - 104 PhotoScroller

前端 未结 14 1768
野性不改
野性不改 2020-12-12 13:28

I am going through the Sample code of iPhone WWDC 2010 - 104 PhotoScroller App. It\'s working great with my project related images (PDF Page Images)

but I am struggl

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 14:09

    Swift 3

    Normally image zoom functionality is required with both double tap and pinch gesture, so I am providing complete solution to achieve the same. Double tap zoom is inspired by above answers and pinch zoom was taken from here.

    import UIKit
    
    class ViewController: UIViewController, UIScrollViewDelegate {
    
        var imageView: UIImageView!
        var scrollImg: UIScrollView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let vWidth = self.view.frame.width
            let vHeight = self.view.frame.height
    
            scrollImg = UIScrollView()
            scrollImg.delegate = self
            scrollImg.frame = CGRect(x: 0, y: 0, width: vWidth, height: vHeight)
            scrollImg.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
            scrollImg.alwaysBounceVertical = false
            scrollImg.alwaysBounceHorizontal = false
            scrollImg.showsVerticalScrollIndicator = true
            scrollImg.flashScrollIndicators()
    
            scrollImg.minimumZoomScale = 1.0
            scrollImg.maximumZoomScale = 10.0
    
            let doubleTapGest = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTapScrollView(recognizer:)))
            doubleTapGest.numberOfTapsRequired = 2
            scrollImg.addGestureRecognizer(doubleTapGest)
    
            self.view.addSubview(scrollImg)
    
            imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: vWidth, height: vHeight))
            imageView.image = UIImage(named: "cat")
            imageView!.layer.cornerRadius = 11.0
            imageView!.clipsToBounds = false
            scrollImg.addSubview(imageView!)
        }
    
        func handleDoubleTapScrollView(recognizer: UITapGestureRecognizer) {
            if scrollImg.zoomScale == 1 {
                scrollImg.zoom(to: zoomRectForScale(scale: scrollImg.maximumZoomScale, center: recognizer.location(in: recognizer.view)), animated: true)
            } else {
                scrollImg.setZoomScale(1, animated: true)
            }
        }
    
        func zoomRectForScale(scale: CGFloat, center: CGPoint) -> CGRect {
            var zoomRect = CGRect.zero
            zoomRect.size.height = imageView.frame.size.height / scale
            zoomRect.size.width  = imageView.frame.size.width  / scale
            let newCenter = imageView.convert(center, from: scrollImg)
            zoomRect.origin.x = newCenter.x - (zoomRect.size.width / 2.0)
            zoomRect.origin.y = newCenter.y - (zoomRect.size.height / 2.0)
            return zoomRect
        }
    
        func viewForZooming(in scrollView: UIScrollView) -> UIView? {
            return self.imageView
        }
    
    }
    

提交回复
热议问题