swift3.0 图片放大缩小动画效果

swift3.0 图片放大缩小动画效果,第1张

概述一. 内容说明       跟我之前这篇类似,只不过那篇是OC版本,这篇是Swift版本 OC版本链接地址 目的:通过kingfisher请求5张图片,展示出来。然后利用图片放大缩小管理类展示图片,多张图片可以滑动浏览 效果图如下,想看动态的效果图,请看上面链接中的OC版本效果图,跟这篇是一样的。 本demo,只加载本地图片的demo下载链接 ,需要加载网络图片的,需要下载Kingfisher 二

一. 内容说明

跟我之前这篇类似,只不过那篇是OC版本,这篇是Swift版本OC版本链接地址

目的:通过kingfisher请求5张图片,展示出来。然后利用图片放大缩小管理类展示图片,多张图片可以滑动浏览

效果图如下,想看动态的效果图,请看上面链接中的OC版本效果图,跟这篇是一样的。

本demo,只加载本地图片的demo下载链接,需要加载网络图片的,需要下载Kingfisher



二.源码展示

0. 图片测试demo源码

import Foundationimport UIKitclass LJPhotoGroupVIEwController : TFBaseVIEwController{    lazy var ljArray : [LJPhotoInfo] = [LJPhotoInfo]()    let ljUrlArray = ["http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg","http://d.lanrentuku.com/down/png/1706/10avatars-material-pngs/avatars-material-man-4.png","http://image.nationalgeographic.com.cn/2017/0703/20170703042329843.jpg","http://image.nationalgeographic.com.cn/2015/0121/20150121033625957.jpg","http://image.nationalgeographic.com.cn/2017/0702/20170702124619643.jpg"]        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()                self.settopNavbarTitle("图片浏览测试Demo")        self.settopNavBackbutton()        self.setUI()    }}extension LJPhotoGroupVIEwController{    func setUI(){               for index in 0...4{                        //1.加载本地图片            //let image = UIImage.init(named: "\(index + 1).jpg")            let showImageVIEw = UIImageVIEw.init()            //showImageVIEw.image = image            showImageVIEw.tag = index            showImageVIEw.frame = CGRect(x: Int((AppWIDth - 200)/2.0),y: 80 + Int(90 * index),wIDth: 200,height: 80)            showImageVIEw.isUserInteractionEnabled = true            vIEw.addSubvIEw(showImageVIEw)                        //2.加载本地图片            let url = URL(string:ljUrlArray[index])            showImageVIEw.kf.setimage(with: url)                        let gestrue = UITapGestureRecognizer.init(target: self,action: #selector(LJPhotoGroupVIEwController.showClicked(_:)))            showImageVIEw.addGestureRecognizer(gestrue)                        //需要浏览的图片添加到数组            let info = LJPhotoInfo.init()            info.largeImageURLStr = ljUrlArray[index]            info.thumbImagevIEw = showImageVIEw            info.currentSelectIndex = index            self.ljArray.append(info)        }    }}extension LJPhotoGroupVIEwController{        func showClicked(_ sender : UITapGestureRecognizer){        if self.ljArray.count > 0 {            let index = sender.vIEw?.tag            let photoGroupVIEw = LJPhotoGroupVIEw.init(frame: CGRect(x: 0,y: 0,wIDth: AppWIDth,height: AppHeight))            photoGroupVIEw.setData(self.ljArray,selectedindex: index!)            photoGroupVIEw.showPhotoVIEw()                        CHDeBUGLog("-------\(String(describing: index))")        }    }}


1.LJPhotoGroupVIEw:图片浏览管理类,用于展示图片

import Foundationimport UIKitclass LJPhotoGroupVIEw: UIVIEw {        let baseIndex = 1000        var originFrame : CGRect? // 图片的源尺寸    var currentIndex : NSInteger = 0 //当前选中的图片index    var ljPhotoArray : [Any] = [Any]()//存储多组需要加载的图片原始信息        lazy var lJscrollVIEw : UIScrollVIEw = {        let vIEw  = UIScrollVIEw.init(frame: CGRect(x: 0,height: AppHeight))        vIEw.delegate = self        vIEw.isPagingEnabled = true        vIEw.backgroundcolor = UIcolor.yellow        return vIEw    }()        overrIDe init(frame: CGRect) {        super.init(frame: frame)        self.addSubvIEw(self.lJscrollVIEw)    }    func setData(_ photoArray : Array<Any>,selectedindex : NSInteger) {        self.lJscrollVIEw.contentSize = CGSize(wIDth: floor(AppWIDth) * CGfloat(photoArray.count),height: AppHeight)        self.currentIndex = selectedindex        self.ljPhotoArray = photoArray    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}extension LJPhotoGroupVIEw {// MARK: -- 图片cell复用    func dequeueReusableCell() -> LJPhotoVIEw {                var cell = self.vIEwWithTag(baseIndex + self.currentIndex) as? LJPhotoVIEw                if ljPhotoArray.count > currentIndex {            let info = ljPhotoArray[currentIndex] as? LJPhotoInfo            let tempImageVIEw = info?.thumbImagevIEw            if cell != nil{                self.originFrame = tempImageVIEw?.convert((tempImageVIEw?.bounds)!,to: self)                return cell!            }                        cell = LJPhotoVIEw.init(frame: CGRect(x: floor(AppWIDth)*CGfloat(currentIndex),height: AppHeight))            self.originFrame = tempImageVIEw?.convert((tempImageVIEw?.bounds)!,to: self)        }        return cell!    }  // MARK: -- 展示图片    func showPhotoVIEw(){                UIApplication.shared.keyWindow?.rootVIEwController?.vIEw.addSubvIEw(self)        self.backgroundcolor = UIcolor.black            let cell1 = self.dequeueReusableCell()        cell1.tag = self.baseIndex + self.currentIndex                var ljTempImage : UIImage?        if ljPhotoArray.count > currentIndex {            let info = ljPhotoArray[currentIndex] as? LJPhotoInfo            ljTempImage = info?.thumbImagevIEw?.image        }                ljTempImage = (ljTempImage != nil) ? ljTempImage : UIImage.init(named: "pic_broadcast_gray_square")                let tfImageVIEw  = UIImageVIEw.init(image: ljTempImage)        tfImageVIEw.frame = self.originFrame ?? CGRect.zero        tfImageVIEw.clipsToBounds = true        tfImageVIEw.backgroundcolor = UIcolor.red        tfImageVIEw.contentMode = .scaleAspectFit        self.addSubvIEw(tfImageVIEw)                //添加页面消失的手势        let tap = UITapGestureRecognizer.init(target: self,action: #selector(hIDeImageVIEw))        self.addGestureRecognizer(tap)                UIVIEw.animate(withDuration: 0.25,animations: {            let y : CGfloat? = (AppHeight - (ljTempImage?.size.height)! * AppWIDth / (ljTempImage?.size.wIDth)!)/2.0            let height : CGfloat? = (ljTempImage?.size.height)! * AppWIDth / (ljTempImage?.size.wIDth)!            tfImageVIEw.frame = CGRect(x: 0,y: y!,height: height!)        }) { (finish) in            //根据选中第几张图片直接展示出来            let cell = self.dequeueReusableCell()            cell.tag = self.baseIndex + self.currentIndex            cell.backgroundcolor = UIcolor.gray                        if self.ljPhotoArray.count > self.currentIndex{                cell.setCurrentimagevIEw(self.ljPhotoArray[self.currentIndex] as! LJPhotoInfo)            }            let x : CGfloat = CGfloat(self.currentIndex) * floor(AppWIDth);            self.lJscrollVIEw.setContentOffset(CGPoint.init(x: x,y: 0),animated: false)            self.lJscrollVIEw.addSubvIEw(cell)                                    tfImageVIEw.removeFromSupervIEw()        }    }    // MARK: -- 移除图片    func hIDeImageVIEw(){        let cell = self.vIEwWithTag(baseIndex + currentIndex) as? LJPhotoVIEw        UIVIEw.animate(withDuration: 0.25,animations: {            cell?.ljImageVIEw.frame = self.originFrame!        }) { (finish) in            self.backgroundcolor = UIcolor.white            self.removeFromSupervIEw()        }    }}extension LJPhotoGroupVIEw : uiscrollviewdelegate{       func scrollVIEwDIDScroll(_ scrollVIEw: UIScrollVIEw) {        //滑动时,会调用多次    }        func scrollVIEwDIDEndDecelerating(_ scrollVIEw: UIScrollVIEw) {     //滑动完毕时,只会调用一次        let page = self.lJscrollVIEw.contentOffset.x / self.frame.size.wIDth;        self.currentIndex = NSInteger(page);        print("scrollVIEwDIDEndDecelerating当前页数----\(page)")                let cell = self.dequeueReusableCell()        cell.tag = self.baseIndex + Int(page)        if self.ljPhotoArray.count > self.currentIndex{            cell.setCurrentimagevIEw(self.ljPhotoArray[self.currentIndex] as! LJPhotoInfo)        }        self.lJscrollVIEw.addSubvIEw(cell)    }}


2.LJPhotoInfo:图片信息的model

import Foundationimport UIKitclass LJPhotoInfo: NSObject {        var currentSelectIndex : Int?    var largeImageURLStr : String?    var thumbImagevIEw : UIImageVIEw?        overrIDe init() {        super.init()    }}


3.LJPhotoVIEw:图片浏览管理类用到的cell(图片显示)

import Foundationimport UIKitclass LJPhotoVIEw: UIScrollVIEw {        var ljInfo : LJPhotoInfo?        lazy var ljImageVIEw : UIImageVIEw = {            let vIEw = UIImageVIEw()            vIEw.clipsToBounds = true            vIEw.contentMode = .scaleAspectFit            return vIEw        }()        overrIDe init(frame: CGRect) {        super.init(frame: frame)        self.zoomScale = 1.0        self.addSubvIEw(self.ljImageVIEw)    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}extension LJPhotoVIEw{    func setCurrentimagevIEw(_ info : LJPhotoInfo){        self.ljInfo = info        if self.ljInfo?.thumbImagevIEw?.image == nil{           self.ljInfo?.thumbImagevIEw?.image = UIImage.init(named: "pic_broadcast_gray_square")        }                //无url,则通过thumbImagevIEw获取Image展示        //self.ljImagevIEw.image = info.thumbImagevIEw.image;        let y : CGfloat? = (AppHeight - (info.thumbImagevIEw?.image?.size.height)! * AppWIDth / (info.thumbImagevIEw?.image?.size.wIDth)!) * 0.5;        self.ljImageVIEw.frame = CGRect(x: 0,height: AppWIDth*(info.thumbImagevIEw?.image?.size.height)!/(info.thumbImagevIEw?.image?.size.wIDth)!)        self.ljImageVIEw.image = self.ljInfo?.thumbImagevIEw?.image                if info.largeImageURLStr != "" {            let url = URL(string:info.largeImageURLStr!)            self.ljImageVIEw.kf.setimage(with: url)        }    }}
总结

以上是内存溢出为你收集整理的swift3.0 图片放大缩小动画效果全部内容,希望文章能够帮你解决swift3.0 图片放大缩小动画效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存