图片点击全屏预览+缩放(Swift 5.0)

房东的猫 提交于 2020-04-24 02:23:10

图片点击全屏预览+缩放(Swift 5.0),直接上代码:

import UIKit

    class ViewController: UIViewController ,UIScrollViewDelegate {
        var scrollView:UIScrollView?
        var lastImageView:UIImageView?
        var originalFrame:CGRect!
        var isDoubleTap:ObjCBool!
        //使用sb拖控件显示图片
        @IBOutlet weak var myImageView: UIImageView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            myImageView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(showZoomImageView(tap:)))
            self.myImageView.addGestureRecognizer(tap)
        }
        
        @objc func showZoomImageView( tap : UITapGestureRecognizer) {
            let bgView = UIScrollView.init(frame: UIScreen.main.bounds)
            bgView.backgroundColor = UIColor.black
            let tapBg = UITapGestureRecognizer.init(target: self, action: #selector(tapBgView(tapBgRecognizer:)))
            bgView.addGestureRecognizer(tapBg)
            let picView = tap.view as! UIImageView//view 强制转换uiimageView
            let imageView = UIImageView.init()
            imageView.image = picView.image;
            imageView.frame = bgView.convert(picView.frame, from: self.view)
            bgView.addSubview(imageView)
            UIApplication.shared.keyWindow?.addSubview(bgView)
            self.lastImageView = imageView
            self.originalFrame = imageView.frame
            self.scrollView = bgView
            self.scrollView?.maximumZoomScale = 1.5
            self.scrollView?.delegate = self
            
            UIView.animate(
                withDuration: 0.5,
                delay: 0.0,
                options: UIView.AnimationOptions.beginFromCurrentState,
                animations: {
                    var frame = imageView.frame
                    frame.size.width = bgView.frame.size.width
                    frame.size.height = frame.size.width * ((imageView.image?.size.height)! / (imageView.image?.size.width)!)
                    frame.origin.x = 0
                    frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5
                    imageView.frame = frame
                }, completion: nil
            )
            
        }
        
        @objc func tapBgView(tapBgRecognizer:UITapGestureRecognizer)
        {
            self.scrollView?.contentOffset = CGPoint.zero
            UIView.animate(withDuration: 0.5, animations: { 
                self.lastImageView?.frame = self.originalFrame
                tapBgRecognizer.view?.backgroundColor = UIColor.clear
            }) { (finished:Bool) in
                tapBgRecognizer.view?.removeFromSuperview()
                self.scrollView = nil
                self.lastImageView = nil
            }
        }
        
        //正确代理回调方法
        func viewForZooming(in scrollView: UIScrollView) -> UIView? {
            return self.lastImageView
        }
        
        //错误代理回调方法
    //    func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
    //        return self.lastImageView
    //    }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    }

 

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