iOS动画-扩散波纹效果
最终效果


实现思路
动画的表现形式是颜色以及大小的变化,整体效果可以看做多个单独的波纹效果的叠加。因此我们可以创建多个CALayer,分别赋予CABasicAnimation动画,组成最终的动画效果。
因此我们先从单个波纹扩散效果来尝试,然后根据时间差将效果叠加起来。
代码
1.新建动画 View RippleAnimationView,动画效果在animationLayer上实现。
新建RippleAnimationView类,继承自UIView,设置扩散倍数,然后重写- (void)drawRect:(CGRect)rect方法,在方法内部新建承载动画的animationLayer。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #import@interfaceRippleAnimationView : UIView/** 设置扩散倍数。默认1.423倍 */@property (nonatomic, assign) CGFloat multiple;- (instancetype)initWithFrame:(CGRect)frame;@end@implementation RippleAnimationView- (instancetype)initWithFrame:(CGRect)frame {    self = [superinitWithFrame:frame];        if(self) {        self.backgroundColor = [UIColor clearColor];       _multiple = 1.423;    }        returnself;}- (void)drawRect:(CGRect)rect {        CALayer *animationLayer = [CALayer layer];        // 加入动画        [self.layer addSublayer:animationLayer];} | 
2.创建单个扩散的动画承载CALayer,实现扩散效果。
首先实现缩放动画
| 1 2 3 4 5 6 7 8 9 10 | - (CABasicAnimation *)scaleAnimation {    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];        scaleAnimation.fromValue = @1;    scaleAnimation.toValue = @(_multiple);    scaleAnimation.beginTime = CACurrentMediaTime();    scaleAnimation.duration = 3;    scaleAnimation.repeatCount = HUGE;// 重复次数设置为无限    returnscaleAnimation;} | 
新建CALayer,并在layer上加载动画。然后将这个Layer放在animationLayer上。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | - (void)drawRect:(CGRect)rect {        CALayer *animationLayer = [CALayer layer];        // 新建缩放动画    CABasicAnimation *animation = [self scaleAnimation];        // 新建一个动画 Layer,将动画添加上去    CALayer *pulsingLayer = [self pulsingLayer:rect animation:animation];        //将动画 Layer 添加到 animationLayer    [animationLayer addSublayer:pulsingLayer];        [self.layer addSublayer:animationLayer];}- (CALayer *)pulsingLayer:(CGRect)rect animation:(CABasicAnimation *)animation {    CALayer *pulsingLayer = [CALayer layer];        pulsingLayer.borderWidth = 0.5;    pulsingLayer.borderColor = [UIColor blackColor].CGColor;    pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);    pulsingLayer.cornerRadius = rect.size.height / 2;    [pulsingLayer addAnimation:animation forKey:@"plulsing"];        returnpulsingLayer;} | 
可以看看现在的效果是这样的

3. 加入背景色以及边框色的渐变效果,将单一的缩放动画合并为动画组CAAnimationGroup。
(ps: 除了改变背景色,还要设置并改变边框色的更主要原因是去除锯齿)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | // 设置一个初始化颜色的宏#define ColorWithAlpha(r,g,b,a) [UIColor colorWithRed:r/255.0green:g/255.0blue:b/255.0alpha:a]- (void)drawRect:(CGRect)rect {        CALayer *animationLayer = [CALayer layer];        // 这里同时创建[缩放动画、背景色渐变、边框色渐变]三个简单动画    NSArray *animationArray = [self animationArray];        // 将三个动画合并为一个动画组    CAAnimationGroup *animationGroup = [self animationGroupAnimations:animationArray];        //修改方法,将原先添加的动画由“简单动画”改为“动画组”    CALayer *pulsingLayer = [self pulsingLayer:rect animation:animationGroup];        //将动画 Layer 添加到 animationLayer    [animationLayer addSublayer:pulsingLayer];    [self.layer addSublayer:animationLayer];}- (NSArray *)animationArray {    NSArray *animationArray = nil;        CABasicAnimation *scaleAnimation = [self scaleAnimation];    CAKeyframeAnimation *borderColorAnimation = [self borderColorAnimation];    CAKeyframeAnimation *backgroundColorAnimation = [self backgroundColorAnimation];    animationArray = @[scaleAnimation, backgroundColorAnimation, borderColorAnimation];        returnanimationArray;}- (CAAnimationGroup *)animationGroupAnimations:(NSArray *)array {    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];        animationGroup.beginTime = CACurrentMediaTime();    animationGroup.duration = 3;    animationGroup.repeatCount = HUGE;    animationGroup.animations = array;    animationGroup.removedOnCompletion = NO;    returnanimationGroup;}- (CABasicAnimation *)scaleAnimation {    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];        scaleAnimation.fromValue = @1;    scaleAnimation.toValue = @(_multiple);    returnscaleAnimation;}// 使用关键帧动画,使得颜色动画不要那么的线性变化- (CAKeyframeAnimation *)backgroundColorAnimation {    CAKeyframeAnimation *backgroundColorAnimation = [CAKeyframeAnimation animation];        backgroundColorAnimation.keyPath = @"backgroundColor";    backgroundColorAnimation.values = @[(__bridge id)ColorWithAlpha(255, 216, 87, 0.5).CGColor,                                        (__bridge id)ColorWithAlpha(255, 231, 152, 0.5).CGColor,                                        (__bridge id)ColorWithAlpha(255, 241, 197, 0.5).CGColor,                                        (__bridge id)ColorWithAlpha(255, 241, 197, 0).CGColor];    backgroundColorAnimation.keyTimes = @[@0.3,@0.6,@0.9,@1];    returnbackgroundColorAnimation;}- (CAKeyframeAnimation *)borderColorAnimation {    CAKeyframeAnimation *borderColorAnimation = [CAKeyframeAnimation animation];        borderColorAnimation.keyPath = @"borderColor";    borderColorAnimation.values = @[(__bridge id)ColorWithAlpha(255, 216, 87, 0.5).CGColor,                                    (__bridge id)ColorWithAlpha(255, 231, 152, 0.5).CGColor,                                    (__bridge id)ColorWithAlpha(255, 241, 197, 0.5).CGColor,                                    (__bridge id)ColorWithAlpha(255, 241, 197, 0).CGColor];    borderColorAnimation.keyTimes = @[@0.3,@0.6,@0.9,@1];    returnborderColorAnimation;}- (CALayer *)pulsingLayer:(CGRect)rect animation:(CAAnimationGroup *)animationGroup {    CALayer *pulsingLayer = [CALayer layer];        pulsingLayer.borderWidth = 0.5;    pulsingLayer.borderColor = ColorWithAlpha(255, 216, 87, 0.5).CGColor;    pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);    pulsingLayer.cornerRadius = rect.size.height / 2;    [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];    returnpulsingLayer;} | 
现在就有种渐变的感觉了

4. 同时创建三个扩散动画的CALyer,将开始动画的时间错开,同时添加到animationLayer上。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // 设置静态常量 pulsingCount ,表示 Layer 的数量staticNSInteger constpulsingCount = 3;// 设置静态常量 animationDuration ,表示动画时间staticdouble constanimationDuration = 3;- (void)drawRect:(CGRect)rect {        CALayer *animationLayer = [CALayer layer];        // 利用 for 循环创建三个动画 Layer    for(inti = 0; i < pulsingCount; i++) {        NSArray *animationArray = [self animationArray];                // 通过传入参数 i 计算,错开动画时间        CAAnimationGroup *animationGroup = [self animationGroupAnimations:animationArray index:i];        CALayer *pulsingLayer = [self pulsingLayer:rect animation:animationGroup];        [animationLayer addSublayer:pulsingLayer];    }    [self.layer addSublayer:animationLayer];}... ...- (CAAnimationGroup *)animationGroupAnimations:(NSArray *)array index:(int)index {    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];        animationGroup.beginTime = CACurrentMediaTime() + (double)(index * animationDuration) / (double)pulsingCount;    animationGroup.duration = animationDuration;    animationGroup.repeatCount = HUGE;    animationGroup.animations = array;    animationGroup.removedOnCompletion = NO;    returnanimationGroup;}... ... | 
然后效果有点……一言难尽……

真是很有纪律性的变化啊~~好吧,只需要加入动画曲线就好了
5. 最后加入动画速度曲线
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ... ...- (CAAnimationGroup *)animationGroupAnimations:(NSArray *)array index:(int)index {    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];        animationGroup.beginTime = CACurrentMediaTime() + (double)(index * animationDuration) / (double)pulsingCount;    animationGroup.duration = animationDuration;    animationGroup.repeatCount = HUGE;    animationGroup.animations = array;    animationGroup.removedOnCompletion = NO;        // 添加动画曲线。关于其他的动画曲线,也可以自行尝试    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];    returnanimationGroup;}... ... | 
如果需要点扩散,那就设置 frame 极小,同时扩散倍数增大即可。
将动画View垫在另一个圆形View之下即可实现最上方的效果。关闭背景色,重调边框色和边框宽度即可实现第二种效果。
iOS动画-扩散波纹效果的更多相关文章
- css3动画图片波纹效果
		这里的图片很有特点,下面有演示图片样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ... 
- ios 动画效果CATransition笔记
		初学ios开发,很多概念还不清楚,所以只有边学边做例子.又怕学了后面忘了前面,因此用自己的博客来纪录自己的学习历程,也是对自己学习不要懈怠做个监督. 刚学ios做动画效果.因为ios封装得很好,实现i ... 
- iOS动画开发之五——炫酷的粒子效果
		在上几篇博客中,我们对UIView层的动画以及iOS的核心动画做了介绍,基本已经可以满足iOS应用项目中所有的动画需求,如果你觉得那些都还不够炫酷,亦或是你灵光一现,想用UIKit框架写出一款炫酷的休 ... 
- ios点击产生波纹效果
		ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ... 
- iOS开发——图形编程OC篇&粘性动画以及果冻效果
		粘性动画以及果冻效果 在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲 ... 
- IOS 动画专题   --iOS核心动画
		iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看 ... 
- iOS动画进阶 - 手摸手教你写ShineButton动画
		移动端访问不佳,请访问我的个人博客 前段时间在github上看见一个非常nice的动画效果,可惜是安卓的,想着用swift写一个iOS版的,下下来源代码研究了一下,下面是我写代码的心路历程 先上图和d ... 
- Android自定义组件系列【14】——Android5.0按钮波纹效果实现
		今天任老师发表了一篇关于Android5.0中按钮按下的波纹效果实现<Android L中水波纹点击效果的实现>,出于好奇我下载了源代码看了一下效果,正好手边有一个Nexus手机,我结合实 ... 
- jquery ripples水波纹效果( 涟漪效果)
		这个效果是我从bootstrap-material-design上面分离下来的,bootstrap-material-design的一些组件样式我不太不喜欢,但是非常喜欢这个水波纹效果,所以就有了这篇 ... 
随机推荐
- fiddle连接终端测试配置
			第一次做app,对app的数据要进行一些数据抓包和数据分析,知道客户端发送到服务器端的过程和逻辑,通过抓包了解和分析出错,前提要先连接fiddle 
- POJ 3734 Blocks (线性递推)
			定义ai表示红色和绿色方块中方块数为偶数的颜色有i个,i = 0,1,2. aij表示刷到第j个方块时的方案数,这是一个线性递推关系. 可以构造递推矩阵A,用矩阵快速幂求解. /*********** ... 
- kafka 开机启动脚本
			/etc/init.d$ vi kafka-start-up.sh #!/bin/bash #export KAFKA_HOME=$PATH export KAFKA_HOME=/opt/Kafka/ ... 
- cloudera manager的卸载以及重新安装
			1 卸载cloudera 参照 http://www.cnblogs.com/chenfool/p/3738540.html Cloudera 的官方介绍: http://www.cloudera.c ... 
- ThreadLocal为什么要用WeakReference
			先上一张图看一下ThreadLocal的内部结构,每个Thread对象内部都维护了一个ThreadLocal.ThreadLocalMap 我们在上图看到的就是三个Thread对象内部格子的Threa ... 
- JS中进行浮点数计算式,遇到的问题
			今天在做项目时,需要在页面进行计算,但是当两个数都是小数时,计算的结果却不是想象中的: 比如1371.3-0.9算出来却是1370.39999999,后来上网搜一下,原来js是弱类型语言,没有那么高的 ... 
- React后台管理系统-file-uploader组件
			1.React文件上传组件github地址: https://github.com/SoAanyip/React-FileUpload 2.Util里边新建file-uploader文件夹,里边新建i ... 
- 支持无限加载的js图片画廊插件
			natural-gallery-js是一款支持无限加载的js图片画廊插件.该js图片画廊支持图片的懒加载,可以对图片进行搜索,分类,还可以以轮播图的方式来展示和切换图片. 使用方法 在页面中引入下面的 ... 
- 【Django】使用list对单个或者多个字段求values值
			使用list对values进行求值: 单个字段的输出结果: price_info=list(Book.objects.filter(auth_id='Yu').values('book_price') ... 
- eclipse projectExplorer视图(以包的方式显示)与navigator视图切换(以文件夹的方式显示)及树状视图与平面视图的切换
			projectExplorer与navigator的切换 projectExplorer视图效果 想要此视图效果步骤如下: 分割------------------------------------ ... 
