swift 一句代码调用imageView点击缩放图片

最近在用swift仿写今日头条,利用Charles抓包工具,这里有详细使用方法,

写了一个图片点击放大的UIImageView的分类
首先创建分类,由于我刚学swift,还没有找到可以直接创建分类的方法,所以创建分类的时候我是直接创建swift文件,然后自己添加,然后利用runtime 给分类添加属性
话不多说,直接上代码,使用的时候自己创建一个swift file 空文件,把以下代码复制粘贴过去,直接调用

//cell 上的 图片点击缩放
 cell.picImageView.scaleImageView()
import UIKit

extension UIImageView :UIScrollViewDelegate {//这里继承代理
 //给分类添加属性
    var originImageRect: CGRect? {
        set {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key".hashValue)
            objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        
        get {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key".hashValue)
            let obj: CGRect? = objc_getAssociatedObject(self, key) as? CGRect
            return obj
        }
    }
    
    
    var container_ScrollView: UIScrollView? {
        set {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key1".hashValue)
            objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
        get {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key1".hashValue)
            let obj: UIScrollView? = objc_getAssociatedObject(self, key) as? UIScrollView
            return obj
        }
    }
    
    
    var container_View: UIView? {
        set {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key2".hashValue)
            objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
        get {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key2".hashValue)
            let obj: UIView? = objc_getAssociatedObject(self, key) as? UIView
            return obj
        }
    }
    
    var snapShotView: UIView? {
        set {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key3".hashValue)
            objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
        get {
            let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "key3".hashValue)
            let obj: UIView? = objc_getAssociatedObject(self, key) as? UIView
            return obj
        }
    }

//点击缩放手势
    func scaleImageView() {
        let window = UIApplication.shared.keyWindow
        
        let image = self.image
        
        originImageRect = self .convert(self.bounds, to: window)
        
        container_ScrollView = UIScrollView(frame: CGRect( x: 0, y :0,width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        
        container_ScrollView?.delegate = self;
        
        container_ScrollView?.isMultipleTouchEnabled = true
        
        container_ScrollView?.maximumZoomScale = 3.0
        
        
        container_View = UIView()
        container_View?.frame = (container_ScrollView?.frame)!
        container_View?.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0)
        
        snapShotView = self.snapshotView(afterScreenUpdates: true)
        snapShotView?.frame = originImageRect!
        
        container_View?.addSubview(snapShotView!)
        container_ScrollView?.addSubview(container_View!)
        window?.addSubview(container_ScrollView!)
        
        container_ScrollView?.addTarget(target: self, action: #selector(self.hideImage))
        
        container_ScrollView?.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0)
        
        let rate =  UIScreen.main.bounds.size.width / (image?.size.width)!
        
        let finalRect = CGRect(x:0, y:( UIScreen.main.bounds.size.height - (image?.size.height)! * rate ) / 2, width:UIScreen.main.bounds.size.width,height:(image?.size.height)! * rate)
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { 
            self.snapShotView?.frame = finalRect
            self.container_ScrollView?.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 1.0)
        }) { (finished) in
            
        }
        
    }
   
    //缩放隐藏
    func hideImage() {
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
            self.container_ScrollView?.zoomScale = 1.0
            self.snapShotView?.frame = self.originImageRect!
            self.container_ScrollView?.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0)
            self.container_ScrollView?.removeFromSuperview()
        }) { (finished) in
            
        }
        
    }
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        
        return container_View
    }
}

但是暂时还不支持长图片的预览,以后再更新

推荐阅读更多精彩内容