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 ...
随机推荐
- 好用的图片缩放JS
<!DOCTYPE HTML> <meta charset="UTF-8"> <head> <script src="jquer ...
- PyQt环境配置
1.下载python 登录Python官网,目前最新的版本是3.6.3,网址为:https://www.python.org/downloads/release/python-363/ 选中Windo ...
- linux环境中安装iotop命令,解决-bash: iotop: command not found问题
需求描述: 今天在测试环境中,准备查看mysql各个线程占用的io的情况,准备使用iotop命令来查看,发现没有这个命令 [root@testvm Packages]# iotop -bash: io ...
- golang程序在windows上,注册为服务
https://blog.csdn.net/yang8023tao/article/details/53332984
- getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)
今天捣鼓了一下午.getActionBar().setDisplayHomeAsUpEnabled(true)总是报空指针.在我的还有一个Android4.4.2的项目中就没有一点问题.我还以为是我自 ...
- Spring-----配置及对象初始化(1)
一,配置文件进行Spring初始化 1,配置文件编写 <?xml version="1.0" encoding="utf-8" ?> <con ...
- C#反射基础理解1(转)
反射提供了封装程序集.模块和类型的对象(Type类型) 可以使用反射动态的创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后,可以调用类型的方法或访问其字段和属性 . 总之,有了反射, ...
- python中交换两个值的方法
a = 4b = 5 #第1种c = 0c = aa = bb = c #第2种a = a+bb = a-ba = a-b #第3种a,b = b,a 第三种办法本质上是元组之间的赋值 print(& ...
- 【代码审计】Cscms_v4.1 任意文件删除漏洞实例
环境搭建: CSCMS :http://www.chshcms.com/ 网站源码版本:Cscms_v4.1正式版(发布日期:2017-06-05) 程序源码下载:https://github.com ...
- Android开发-- 使用ADT23 的一些问题
在使用最新版ADT 23进行android学习时发现一些问题: 1.通过设置intent的action来启动另外一个activity时,会出现No Activity found to handle I ...