效果就是下面这个样子:

思路借鉴的是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 实现图片点击缩放回弹动画的更多相关文章

  1. iOS Swift WisdomScanKit图片浏览器功能SDK

    iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介      WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...

  2. CSS开发技巧(三):图片点击缩放

    前言  利用CSS实现图片的点击缩放是一个很值得研究的效果.在某些业务需求场景中,我们可能并没有足够的空间展示过大的图片,这就需要限制图片容器的宽度和高度.然而图片限制了宽度,一些图片的细节便又无法看 ...

  3. IOS中实现图片点击全屏预览

    //// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. A ...

  4. [置顶] ios 网页中图片点击放大效果demo

    demo功能:点击网页中的图片,图片放大效果的demo.iphone6.1 测试通过. demo说明:通过webview的委托事件shouldStartLoadWithRequest来实现. demo ...

  5. IOS Swift UITableViewcontroller实现点击空白处隐藏键盘

    在ios开发中,为了方便,我们经常使用UITableViewcontroller,比如搜索界面为了方便可能更多的使用UITableViewcontroller,那么问题就来了,当我点击搜索框的时候会弹 ...

  6. android图片透明度跟缩放大小动画事件

    概序 : 动画事件写在xml中,然后用AnimationUtils去加载动画事件,再监听动画结束事件,隐藏imageview. 1. player_double_click_animation.xml ...

  7. 01-实现图片按钮的缩放、动画效果(block的初步应用)

    #import "ViewController.h" #define kDelta 60 @interface ViewController () @end @implementa ...

  8. iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)       1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...

  9. ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...

随机推荐

  1. Win7下telnet使用

    出于安全考虑,win7已经禁用了telnet这一功能, telnet是明文传输的,安全性很差. 知道了这一点就不奇怪为什么在win7下不能使用telnet了,下面就详细介绍下如何重新开启telnet服 ...

  2. UML类图关系(转,添加了实例)

    UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Associati ...

  3. eclipse debug Liunx服务器上的svn项目

    1.本地项目提交到svn上,以保证本地代码与服务器代码相同 2.开启服务器debug端口 3.使用root账号重新部署服务器项目并监听catalina.out sh /home/p/deploy/gt ...

  4. underscore.js定义模板遇到问题:Uncaught TypeError: Cannot read property 'replace' of undefined

    代码正确缩进位置如下, extend "layout" block 'content',-> div ->'nihao' script id:"Invoice ...

  5. js防止表单重复提交

    1.表单 <form id="addForm" onsubmit="getElementById('submitInput').disabled=true;retu ...

  6. HTML 水平线

    <hr /> 标签可以在 HTML 页面中创建水平线,通常用来分隔文章中的小节 <!DOCTYPE HTML> <html> <body> <p& ...

  7. [JS] 如何自定义字符串格式化输出

    在其他语言中十分常见的字符串格式化输出,居然在 Javascript 中不见踪影,于是决定自己实现该方法,以下就是个人编写的最简洁实现: String.prototype.format = funct ...

  8. MDK972-EK开发板裸调试设置和裸机程序烧写(转)

    硬件平台:MDK972-EK开发板编译调试软件:KEIL uVision4仿真工具:JLINK V7/V8   本例子从串口输出信息,如图:       KEIL uVision4调试设置如图所示: ...

  9. 在MyEclipse(2015)中上传项目到github的步骤(很详细)

    (图文)在MyEclipse(2015)中上传项目到github的步骤(很详细) git|smartGit使用详解 SmartGit使用教程

  10. struts1的配置文件详解

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...