[关闭]
@buoge 2017-05-05T13:59:53.000000Z 字数 2711 阅读 1142

swift 图片点击缩小在复原效果实现

iOS


效果就是下面这个样子:

有图有真相

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

我的调用方式

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


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

  1. func collectionView(_ collectionView: UICollectionView,
  2. shouldHighlightItemAt indexPath: IndexPath) -> Bool {
  3. if let suiteGoodsDetailCell = collectionView.cellForItem
  4. (at: indexPath) as? SuiteGoodsDetailCell{
  5. suiteGoodsDetailCell.animateScaleImg?.touchBeganWithScale()
  6. mSuiteCellDetail?.openSuiteBuyConfirm()
  7. }
  8. return true
  9. }
  10. func collectionView(_ collectionView: UICollectionView,
  11. didUnhighlightItemAt indexPath: IndexPath) {
  12. if let suiteGoodsDetailCell = collectionView.cellForItem
  13. (at: indexPath) as? SuiteGoodsDetailCell{
  14. suiteGoodsDetailCell.animateScaleImg?.touchEndWithScale()
  15. }
  16. }

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

使用方式:

  1. //
  2. // ZZAnimateScaleImg.swift
  3. //
  4. // 实现点击之后图片缩小然后在复原的动画效果
  5. //
  6. // var animateScaleImg: ZZAnimateScaleImg?
  7. //
  8. // Created by buoge on 2017/4/21.
  9. //
  10. import Foundation
  11. class ZZAnimateScaleImg: NSObject,CAAnimationDelegate {
  12. private var isAnimation = false
  13. private var mImageView:UIImageView?
  14. private var imageAnimation: CAKeyframeAnimation?
  15. init(imgView: UIImageView) {
  16. self.mImageView = imgView
  17. }
  18. //CAAnimationDelegate
  19. func animationDidStart(_ anim: CAAnimation) {
  20. isAnimation = true
  21. }
  22. func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
  23. if flag {
  24. isAnimation = false
  25. }
  26. }
  27. // 点击缩小
  28. func touchBeganWithScale() {
  29. if !isAnimation {
  30. UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut, animations: { [weak self] in
  31. self?.mImageView?.transform = CGAffineTransform(scaleX: 0.93, y: 0.93)
  32. }, completion: nil)
  33. }
  34. }
  35. // resetScale
  36. func restScale() {
  37. if !isAnimation {
  38. mImageView?.transform = CGAffineTransform(scaleX: 1, y: 1)
  39. }
  40. }
  41. // 回弹
  42. func touchEndWithScale() {
  43. if !isAnimation {
  44. restScale()
  45. startAnimation()
  46. }
  47. }
  48. // 跳动一下的效果
  49. private func startAnimation() {
  50. imageAnimation = CAKeyframeAnimation(keyPath: "transform")
  51. let scale1 = CATransform3DMakeScale(0.95, 0.95, 1)
  52. let scale2 = CATransform3DMakeScale(0.98, 0.98, 1)
  53. let scale3 = CATransform3DMakeScale(1, 1, 1)
  54. imageAnimation?.values = [scale1,scale2,scale3]
  55. imageAnimation?.keyTimes = [0.05,0.2,1]
  56. imageAnimation?.calculationMode = kCAFilterLinear
  57. imageAnimation?.duration = 0.2
  58. imageAnimation?.repeatCount = 1
  59. imageAnimation?.delegate = self
  60. mImageView?.layer.add(imageAnimation!, forKey: "imageViewEffect")
  61. }
  62. //释放
  63. func removeAnimaton(){
  64. isAnimation = false
  65. imageAnimation?.delegate = nil
  66. mImageView?.layer.removeAllAnimations()
  67. }
  68. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注