• 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()
        }
        
        
    }
    
    
  • 相关阅读:
    Subversion学习笔记
    单元测试 学习笔记 之五
    单元测试 学习笔记 之四
    将全球通讯簿导入pop3客户端联系人
    isa 2006 sp1发布
    使用POWERSHELL管理OCS 2007
    SCCM 2007 排错
    空空排错日志:OCS错误日志14501等解决办法
    在AD没有备份的情况下还原被删除的数据
    冲击波又回来啦?
  • 原文地址:https://www.cnblogs.com/buoge/p/9343369.html
Copyright © 2020-2023  润新知