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 ...
随机推荐
- pycharm pro版本激活
这几天行激活pycharm pro版会遇到问题,license server的网址都不可用了.包括各个网站的百度云的破解资源链接都被和谐了,发现最近jetbrains对盗版管得严. 首次安装在注册界面 ...
- layer的用法
layer的用法: 使用之前必须导入包如下: #import<QuartzCore/QuartzCore.h> 否则使用控件.layer时,是不会提示的,粘贴过来的代码会直接报错. 就拿v ...
- SpringBoot------新建一个项目
1.新建一个Maven Project 2.选择项目路劲 3.选择Web App 4.添加项目名称 5.右击“Properties”->“Java Build Path”->“Source ...
- Struts2_day02讲义_使用Struts完成对客户的新增操作
- gem install cocoapods ERROR: While executing gem ... (Gem::FilePermissionError)
在cocoapods 执行 sudo gem install cocoapods 的时候出现 While executing gem ... (Gem::FilePermissionError) ...
- Oracle 常用函数备查
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- N76E003之ISP
Flash存储器支持硬件编程和应用编程(IAP).如果产品在研发阶段或产品需要更新软固件时,硬件编程就显得不太方便,采用在系统编程(ISP)方式,可使这一过程变得方便.执行ISP不需要将控制器从系统板 ...
- JSPatch实现原理详解
本文转载至 http://blog.cnbang.net/tech/2808/ JSPatch以小巧的体积做到了让JS调用/替换任意OC方法,让iOS APP具备热更新的能力,在实现 JSPatch ...
- RabbitMQ笔记四:Binding,Queue,Message概念
Binding详解 黄线部分就是binding Exchange与Exchange,Queue之间的虚拟连接,Binding中可以包含Routing key或者参数 创建binding 注意: ...
- 【Python3】端口占用监测的程序
#!/usr/bin/env python # -*- coding:utf- -*- #python端口占用监测的程序,该程序可以监测指定IP的端口是否被占用 import socket,time, ...