iOS Swift 实现图片点击缩放回弹动画
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS Swift 实现图片点击缩放回弹动画,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3041字,纯文字阅读大概需要5分钟。
内容图文
效果就是下面这个样子:
思路借鉴的是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()
}
}
原文:https://www.cnblogs.com/buoge/p/9343369.html
内容总结
以上是互联网集市为您收集整理的iOS Swift 实现图片点击缩放回弹动画全部内容,希望文章能够帮你解决iOS Swift 实现图片点击缩放回弹动画所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。