• iOS Swift 实现图片点击缩放回弹动画



    效果就是下面这个样子:

    有图有真相

    思路借鉴的是MZTimerLabel,有想过做一个自定义的ImageView,但那样的话之前view用必须要改代码,索性就按照MZTimerLabel这个方式实现,简单易用,从简从俗

    我的调用方式

    1.CollectionViewCell初始化的时候调用ZZAnimateScaleImg初始化方法
    var animateScaleImg: ZZAnimateScaleImg?
    override func awakeFromNib() {
    super.awakeFromNib()
    animateScaleImg = ZZAnimateScaleImg(imgView: imageIcon)
    }


    2.CollectionView 选中事件中出发动画行为

    func collectionView(_ collectionView: UICollectionView, 
    shouldHighlightItemAt indexPath: IndexPath) -> Bool {
            if let suiteGoodsDetailCell = collectionView.cellForItem
            (at: indexPath) as? SuiteGoodsDetailCell{
                suiteGoodsDetailCell.animateScaleImg?.touchBeganWithScale()
                mSuiteCellDetail?.openSuiteBuyConfirm()
            }
            return true
    }
        
    func collectionView(_ collectionView: UICollectionView, 
    didUnhighlightItemAt indexPath: IndexPath) {
            if let suiteGoodsDetailCell = collectionView.cellForItem
                (at: indexPath) as? SuiteGoodsDetailCell{
                suiteGoodsDetailCell.animateScaleImg?.touchEndWithScale()
            }
        }
    

    3.CollectionView deinit清理资源
    deinit {
    animateScaleImg?.removeAnimaton()
    }

    使用方式:

    • 调用者初始化变量
      var animateScaleImg: ZZAnimateScaleImg?
      animateScaleImg = ZZAnimateScaleImg(imgView: imageIcon)

    • 动画调用
      animateScaleImg?.touchBeganWithScale()
      animateScaleImg?.touchEndWithScale()

    • 调用者释放
      调用者deinit时清理animation对象
      deinit {
      animateScaleImg?.removeAnimaton()
      }

    //
    //  ZZAnimateScaleImg.swift
    //
    //  实现点击之后图片缩小然后在复原的动画效果
    //
    //  var animateScaleImg: ZZAnimateScaleImg?
    //
    //  Created by buoge on 2017/4/21.
    //
    
    
    
    import Foundation
    
    class ZZAnimateScaleImg: NSObject,CAAnimationDelegate {
        
        private var isAnimation = false
        private var mImageView:UIImageView?
        private var imageAnimation: CAKeyframeAnimation?
        
        init(imgView: UIImageView) {
            self.mImageView = imgView
        }
        
        //CAAnimationDelegate
        func animationDidStart(_ anim: CAAnimation) {
            isAnimation = true
        }
        func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
            if flag {
                isAnimation = false
            }
        }
        
        // 点击缩小
        func touchBeganWithScale() {
            if !isAnimation {
                UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut, animations: { [weak self] in
                    self?.mImageView?.transform = CGAffineTransform(scaleX: 0.93, y: 0.93)
                    }, completion: nil)
            }
        }
        
        // resetScale
        func restScale() {
            if !isAnimation {
                mImageView?.transform = CGAffineTransform(scaleX: 1, y: 1)
            }
        }
        
        // 回弹
        func touchEndWithScale() {
            if !isAnimation {
                restScale()
                startAnimation()
            }
        }
        
        // 跳动一下的效果
        private func startAnimation() {
            imageAnimation = CAKeyframeAnimation(keyPath: "transform")
            let scale1 = CATransform3DMakeScale(0.95, 0.95, 1)
            let scale2 = CATransform3DMakeScale(0.98, 0.98, 1)
            let scale3 = CATransform3DMakeScale(1, 1, 1)
            imageAnimation?.values = [scale1,scale2,scale3]
            imageAnimation?.keyTimes = [0.05,0.2,1]
            imageAnimation?.calculationMode = kCAFilterLinear
            imageAnimation?.duration = 0.2
            imageAnimation?.repeatCount = 1
            imageAnimation?.delegate = self
            mImageView?.layer.add(imageAnimation!, forKey: "imageViewEffect")
        }
        
        //释放
        func removeAnimaton(){
            isAnimation = false
            imageAnimation?.delegate = nil
            mImageView?.layer.removeAllAnimations()
        }
        
        
    }
    
    
  • 相关阅读:
    Objective C 绘制透明窗口的方法
    在挂载的 NTFS 盘上运行 gdb 会遇到权限问题,导致无法初始化
    使用 Eclipse 打造 操作系统实习 JOS 开发环境
    C# URL 中文编码与解码
    突破教育网的防线,将搜狗浏览器的教育网加速功能全面推向各种浏览器
    linux 截屏工具
    yum install 时 提示某个已安装的库(x86_64)比某个要安装的库(i686) 新导致安装失败
    全方位打造 Eclipse 自定义开发环境
    自动售饮料机的verilog实现
    数字跑表的verilog实现
  • 原文地址:https://www.cnblogs.com/buoge/p/9343369.html
Copyright © 2020-2023  润新知