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

 更新时间:2021年10月28日 14:00:56   作者:水桶前辈  
这篇文章主要为大家详细介绍了swift3.0实现图片放大缩小动画效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一. 内容说明

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

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

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

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

二.源码展示

0. 图片测试demo源码

import Foundation 
import UIKit 
 
class 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 Foundation 
import UIKit 
 
class 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, y: 0, width: AppWidth, 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), y: 0, width: AppWidth, 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!, width: AppWidth, 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 Foundation 
import UIKit 
 
class LJPhotoInfo: NSObject { 
   
  var currentSelectIndex : Int? 
  var largeImageURLStr : String? 
  var thumbImageview : UIImageView? 
   
  override init() { 
    super.init() 
  } 
} 

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

import Foundation 
import UIKit 
 
class 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, y: y!, width: AppWidth, 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) 
    } 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 一篇文章搞定iOS的Cookie存取

    一篇文章搞定iOS的Cookie存取

    Cookie中文名称叫做“小型文本文件”,指某些网站为了辨别用户身份而存储在用户本地终端上的数据(通常经过加密),下面这篇文章主要给大家介绍了关于iOS的Cookie存取的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • iOS实现圆角箭头视图

    iOS实现圆角箭头视图

    这篇文章主要为大家详细介绍了iOS实现圆角箭头视图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • 详解iOS 实现一对多代理方案

    详解iOS 实现一对多代理方案

    本文主要介绍了iOS 实现一对多代理方案,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • iOS StoreKit 2 新特性盘点解析

    iOS StoreKit 2 新特性盘点解析

    这篇文章主要为大家介绍了iOS StoreKit 2 新特性盘点及要点解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • ios UITableView 自定义右滑删除的实现代码

    ios UITableView 自定义右滑删除的实现代码

    这篇文章主要介绍了ios UITableView 自定义右滑删除的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能

    iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能

    这篇文章主要介绍了iPhone/iPad开发之通过LocalNotification实现iOS定时本地推送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • iOS实现一个意见反馈类型的输入栏

    iOS实现一个意见反馈类型的输入栏

    这篇文章主要给大家介绍了关于利用iOS实现一个意见反馈类型的输入栏,通过文中实现的输入栏会用户一个很好的体验效果,文中给了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-10-10
  • iOS 水波纹动画的实现效果

    iOS 水波纹动画的实现效果

    本篇文章主要介绍了iOS 水波纹的实现的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • iOS App通信之local socket示例

    iOS App通信之local socket示例

    这篇文章主要介绍了iOS App之间的通信 -local socket示例的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS中几种定时器的实现小结

    iOS中几种定时器的实现小结

    这篇文章主要介绍了iOS中几种定时器的实现小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01

最新评论