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()
}
}
iOS Swift 实现图片点击缩放回弹动画的更多相关文章
- iOS Swift WisdomScanKit图片浏览器功能SDK
iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介 WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...
- CSS开发技巧(三):图片点击缩放
前言 利用CSS实现图片的点击缩放是一个很值得研究的效果.在某些业务需求场景中,我们可能并没有足够的空间展示过大的图片,这就需要限制图片容器的宽度和高度.然而图片限制了宽度,一些图片的细节便又无法看 ...
- IOS中实现图片点击全屏预览
//// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. A ...
- [置顶] ios 网页中图片点击放大效果demo
demo功能:点击网页中的图片,图片放大效果的demo.iphone6.1 测试通过. demo说明:通过webview的委托事件shouldStartLoadWithRequest来实现. demo ...
- IOS Swift UITableViewcontroller实现点击空白处隐藏键盘
在ios开发中,为了方便,我们经常使用UITableViewcontroller,比如搜索界面为了方便可能更多的使用UITableViewcontroller,那么问题就来了,当我点击搜索框的时候会弹 ...
- android图片透明度跟缩放大小动画事件
概序 : 动画事件写在xml中,然后用AnimationUtils去加载动画事件,再监听动画结束事件,隐藏imageview. 1. player_double_click_animation.xml ...
- 01-实现图片按钮的缩放、动画效果(block的初步应用)
#import "ViewController.h" #define kDelta 60 @interface ViewController () @end @implementa ...
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
随机推荐
- PHP框架 Yii framework 用yiic命令时提示“php.exe”不是内部或外部命令
解决方案 yii/framework/yiic.bat,修改 if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exei ...
- SQLServer转MYSQL的方法(连数据)[传]
转自 https://blog.csdn.net/AlbenXie/article/details/77449720 SQLServer转MYSQL的方法(连数据) 本次转换需要依赖使用工具Navic ...
- js 获取ISO-8601格式时间字符串的时间戳
function getTimeStamp(isostr) { var parts = isostr.match(/\d+/g); return new Date(parts[0]+'-'+parts ...
- Web暴力破解--前端JS表单加密进行爆破
0x01 前言 常见的js实现加密的方式有:md5.base64.shal,写了一个简单的demo作为测试. 0x02 代码 login.html <!DOCTYPE HTML> < ...
- Outlook 2007 实现自动添加密送的方法
1)在Outlook里面键入Alt+F11打开VBA编辑器: 2)激活左边的工程面板,展开并双击上面的“Project (VbaProject.OTM)/Microsoft Office Ou ...
- [XPath] XPath 与 lxml (五)XPath 实例
本文继续沿用第三章的 XML 示例文档. 选取价格高于30的 price 节点 # 从父节点进行筛选 >>> root.xpath('//book[price>30]/pric ...
- flask + mysql写的简单监控系统
这里以监控内存使用率为例,写的一个简单demo性程序,具体操作根据51reboot提供的教程写如下. 一.建库建表 创建falcon数据库: mysql> create database fal ...
- 使用jquery的 $.grep实现es6的filter效果
现在es6是趋势,习惯了es6会方便的多,但是有时候由于项目的限制,会限制es6的使用,在处理数组的时候,往往会想将一个数组进行过滤产生另一个新数组,如es6的filter 如下,我又一个数组,[1, ...
- iOS UIImage:获取图片主色调
本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...
- gcc的选项
-g: 是一个编译选项,即在源代码编译的过程中起作用,让gcc把更多调试信息(也就包括符号信息)收集起来并将存放到最终的可执行文件内. -rdynamic: 却是一个 连接选项 ,它将指示连接器把 ...