@buoge
2017-05-05T05:59:53.000000Z
字数 2711
阅读 1398
iOS
效果就是下面这个样子:

思路借鉴的是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 Foundationclass ZZAnimateScaleImg: NSObject,CAAnimationDelegate {private var isAnimation = falseprivate var mImageView:UIImageView?private var imageAnimation: CAKeyframeAnimation?init(imgView: UIImageView) {self.mImageView = imgView}//CAAnimationDelegatefunc 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] inself?.mImageView?.transform = CGAffineTransform(scaleX: 0.93, y: 0.93)}, completion: nil)}}// resetScalefunc 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 = kCAFilterLinearimageAnimation?.duration = 0.2imageAnimation?.repeatCount = 1imageAnimation?.delegate = selfmImageView?.layer.add(imageAnimation!, forKey: "imageViewEffect")}//释放func removeAnimaton(){isAnimation = falseimageAnimation?.delegate = nilmImageView?.layer.removeAllAnimations()}}
