美文网首页iOS Swift && Objective-C
swift 点击图片显示大图

swift 点击图片显示大图

作者: flyrr | 来源:发表于2016-09-11 16:05 被阅读533次

外面只需要一行代码就可以点击查看大图,再次点击缩放放回原图。

先定义一个显示图片的ShowHelper类---本来想用struct的,但是貌似不支持添加手势,只有放弃了。。。。。
我现在是能用struct就用struct,很少用class了,来看一下struct的优点---swift.gg上面的

struct优点.png
//放大图片的时间
private let showBigDuration = 0.6
//缩放图片的时间
private let showOriginalDuration = 0.6
class ShowHelper: NSObject {
    /// 图片imageView的原始frame
   private static var originalFrame = CGRect()
    private override init() {
        super.init()
    }
}

然后写一个extension----(为了干净整洁,我定义类/结构体的时候,只写属性),其它方法,我全部放在extension里面---只给外面提供一个show(imageView: UIImageView)方法,其它全部是private私有方法

extension ShowHelper {
//类方法
    class func show(imageView: UIImageView) {
       imageView.userInteractionEnabled = true
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(showBigImage(_:)))
        imageView.addGestureRecognizer(tap)
    }
   
    //private私有方法。。。
   @objc private class func showBigImage(sender: UITapGestureRecognizer) {
//swift里面我不喜欢用强制解析(!)----套用一句话----你每用一个!,就会杀死一只猫
//同时也为了去掉金字塔类型的判断。。。我用guard / guard let来代替if/ if let 
    guard let imageV = sender.view as? UIImageView else {
        fatalError("it is not UIImageView")
    }
    guard let image = imageV.image else {
        return
    }
    guard let window = UIApplication.sharedApplication().delegate?.window else {
        return
    }
//originalFrame重新归零
    originalFrame = CGRect()
    let oldFrame = imageV.convertRect(imageV.bounds, toView: window)
    let backgroundView = UIView(frame: UIScreen.mainScreen().bounds)
    backgroundView.backgroundColor = UIColor.blackColor()
    backgroundView.alpha = 0.0
//赋值originalFrame
    originalFrame = oldFrame
    let newImageV = UIImageView(frame: oldFrame)
    newImageV.contentMode = .ScaleAspectFit
    newImageV.image = image
    backgroundView.addSubview(newImageV)
    window?.addSubview(backgroundView)
    
    UIView.animateWithDuration(showBigDuration) {
        let width = UIScreen.mainScreen().bounds.size.width
        let height = image.size.height * width / image.size.width
        let y = (UIScreen.mainScreen().bounds.size.height - image.size.height * width / image.size.width) * 0.5
        newImageV.frame = CGRectMake(0, y, width, height)
        backgroundView.alpha = 1
    }
    let tap2 = UITapGestureRecognizer(target: self, action: #selector(ShowHelper.showOriginal(_:)))
    backgroundView.addGestureRecognizer(tap2)
    }
    //private私有方法。。。
    @objc private class func showOriginal(sender: UITapGestureRecognizer) {
        guard let backgroundView = sender.view else {
            return
        }
        guard let imageV = backgroundView.subviews.first else {
            return
        }
//大图的frame变为原来的frame,backgroundView的透明度为0,同时backgroundView从父视图移除
        UIView.animateWithDuration(showOriginalDuration, animations: {
            imageV.frame = originalFrame
            backgroundView.alpha = 0.0
            }) { finished in
                backgroundView.removeFromSuperview()
        }
    }
}

相关文章

网友评论

    本文标题:swift 点击图片显示大图

    本文链接:https://www.haomeiwen.com/subject/lhmuettx.html