1. 程式人生 > >iOS Swift 實現圖片點擊縮放回彈動畫

iOS Swift 實現圖片點擊縮放回彈動畫

obj mean awake art 簡單 onf 點擊縮放 fork sde


效果就是下面這個樣子:

技術分享圖片

思路借鑒的是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()
    }
    
    
}

iOS Swift 實現圖片點擊縮放回彈動畫