How to enable zoom in UIScrollView

前端 未结 4 446
小蘑菇
小蘑菇 2020-12-01 09:03

How do I enable zooming in a UIScrollView?

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 09:17

    Have a read through this Ray Wenderlich tutorial:

    http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

    If you follow through the section 'Scrolling and Zooming a Larger Image' it will get a image up and enable you to pinch and zoom.

    In case the link gets altered, here's the main info: Put this code in your view controller (this sets the main functionality):

    override func viewDidLoad() {
      super.viewDidLoad()
    
      // 1
      let image = UIImage(named: "photo1.png")!
      imageView = UIImageView(image: image)
      imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
      scrollView.addSubview(imageView)
    
      // 2
      scrollView.contentSize = image.size
    
      // 3
      var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
      doubleTapRecognizer.numberOfTapsRequired = 2
      doubleTapRecognizer.numberOfTouchesRequired = 1
      scrollView.addGestureRecognizer(doubleTapRecognizer)
    
      // 4
      let scrollViewFrame = scrollView.frame
      let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
      let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
      let minScale = min(scaleWidth, scaleHeight);
      scrollView.minimumZoomScale = minScale;
    
      // 5
      scrollView.maximumZoomScale = 1.0
      scrollView.zoomScale = minScale;
    
      // 6
      centerScrollViewContents()
    }
    

    Add this to the class:

    func centerScrollViewContents() {
      let boundsSize = scrollView.bounds.size
      var contentsFrame = imageView.frame
    
      if contentsFrame.size.width < boundsSize.width {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
      } else {
        contentsFrame.origin.x = 0.0
      }
    
      if contentsFrame.size.height < boundsSize.height {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
      } else {
        contentsFrame.origin.y = 0.0
      }
    
      imageView.frame = contentsFrame
    }
    

    And then this if you want the double tap gesture to be recognised:

    func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
      // 1        
      let pointInView = recognizer.locationInView(imageView)
    
      // 2
      var newZoomScale = scrollView.zoomScale * 1.5
      newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)
    
      // 3
      let scrollViewSize = scrollView.bounds.size
      let w = scrollViewSize.width / newZoomScale
      let h = scrollViewSize.height / newZoomScale
      let x = pointInView.x - (w / 2.0)
      let y = pointInView.y - (h / 2.0)
    
      let rectToZoomTo = CGRectMake(x, y, w, h);
    
      // 4
      scrollView.zoomToRect(rectToZoomTo, animated: true)
    }
    

    If you want more detail read the tutorial, but that pretty much covers it.

提交回复
热议问题