iOS zoom in zoom out 放大缩小图片

首先,在 viewDidLoad 添加设置:

scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 10.0

实现方法一:

    override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
      updateMinZoomScaleForSize(view.bounds.size)
    }
    
    func updateMinZoomScaleForSize(_ size: CGSize) {
      let widthScale = size.width / image.bounds.width
      let heightScale = size.height / image.bounds.height
      let minScale = min(widthScale, heightScale)
        
      scrollView.minimumZoomScale = minScale
      scrollView.zoomScale = minScale
    }


    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return image
    }
    
    // 1 The scroll view calls scrollViewDidZoom(_:) each time the user zooms. In response, you simply call updateConstraintsForSize(_:) and pass in the view’s bounds size.
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
      updateConstraintsForSize(view.bounds.size)
    }

    // 2 updateConstraintsForSize(_:) gets around an annoyance with UIScrollView: If the scroll view’s content size is smaller than its bounds, it places the contents at the top-left of the screen, rather than the center.
    func updateConstraintsForSize(_ size: CGSize) {
      // 3 You center the image vertically by subtracting the height of imageView from the view‘s height and dividing the result in half. This value adds padding to the top and bottom imageView constraints.
      let yOffset = max(0, (size.height - image.frame.height) / 2)
      imageViewTopConstraint.constant = yOffset
      imageViewBottomConstraint.constant = yOffset
      
      // 4 Similarly, you calculate an offset for the leading and trailing constraints of imageView, based on the width of the view.
      let xOffset = max(0, (size.width - image.frame.width) / 2)
      imageViewLeadingConstraint.constant = xOffset
      imageViewTrailingConstraint.constant = xOffset
        
      view.layoutIfNeeded()
    }

实现方法二:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}
    
func scrollViewDidZoom(_ scrollView: UIScrollView) {

   // 有点烧脑
   if scrollView.zoomScale > 1 {
        if let image = imageView.image {
              let rationW = imageView.frame.width / image.size.width
              let rationH = imageView.frame.height / image.size.height
              
              let ratio = rationW < rationH ? rationW : rationH
              let newWidth = image.size.width * ratio
              let newHeight = image.size.height * ratio

              let conditionLeft = newWidth*scrollView.zoomScale > imageView.frame.width
                
              let left = 0.5 * (conditionLeft ? newWidth - imageView.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
                
              let conditionTop = newHeight*scrollView.zoomScale > imageView.frame.height
                
              let top = 0.5 * (conditionTop ? newHeight - imageView.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
                
              scrollView.contentInset = UIEdgeInsets(top:top, left:left, bottom:top, right:left)
                
         }else{
              scrollView.contentInset = .zero
         }
     }
    
}

真机调试,对比证明,第二种实现方式更好一些。

参考:

  1. Zoom image In/Out Programmatically
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值