iOS动画-扩散波纹效果

最终效果

实现思路

动画的表现形式是颜色以及大小的变化,整体效果可以看做多个单独的波纹效果的叠加。因此我们可以创建多个CALayer,分别赋予CABasicAnimation动画,组成最终的动画效果。

因此我们先从单个波纹扩散效果来尝试,然后根据时间差将效果叠加起来。

代码

1.新建动画 View RippleAnimationView,动画效果在animationLayer上实现。

新建RippleAnimationView类,继承自UIView,设置扩散倍数,然后重写- (void)drawRect:(CGRect)rect方法,在方法内部新建承载动画的animationLayer。


#import

@interface RippleAnimationView : UIView

/**

 设置扩散倍数。默认1.423倍

 */

@property (nonatomic, assign) CGFloat multiple;

- (instancetype)initWithFrame:(CGRect)frame;

@end

@implementation RippleAnimationView

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];


    if (self) {

        self.backgroundColor = [UIColor clearColor];

       _multiple = 1.423;

    }


    return self;

}

- (void)drawRect:(CGRect)rect {


    CALayer *animationLayer = [CALayer layer];


    // 加入动画


    [self.layer addSublayer:animationLayer];

}

2.创建单个扩散的动画承载CALayer,实现扩散效果。

首先实现缩放动画


- (CABasicAnimation *)scaleAnimation {

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];


    scaleAnimation.fromValue = @1;

    scaleAnimation.toValue = @(_multiple);

    scaleAnimation.beginTime = CACurrentMediaTime();

    scaleAnimation.duration = 3;

    scaleAnimation.repeatCount = HUGE;// 重复次数设置为无限

    return scaleAnimation;

}

新建CALayer,并在layer上加载动画。然后将这个Layer放在animationLayer上。


- (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"];


    return pulsingLayer;

}

可以看看现在的效果是这样的

3. 加入背景色以及边框色的渐变效果,将单一的缩放动画合并为动画组CAAnimationGroup。

(ps: 除了改变背景色,还要设置并改变边框色的更主要原因是去除锯齿)


// 设置一个初始化颜色的宏

#define ColorWithAlpha(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha: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];


    return animationArray;

}

- (CAAnimationGroup *)animationGroupAnimations:(NSArray *)array {

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];


    animationGroup.beginTime = CACurrentMediaTime();

    animationGroup.duration = 3;

    animationGroup.repeatCount = HUGE;

    animationGroup.animations = array;

    animationGroup.removedOnCompletion = NO;

    return animationGroup;

}

- (CABasicAnimation *)scaleAnimation {

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];


    scaleAnimation.fromValue = @1;

    scaleAnimation.toValue = @(_multiple);

    return scaleAnimation;

}

// 使用关键帧动画,使得颜色动画不要那么的线性变化

- (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];

    return backgroundColorAnimation;

}

- (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];

    return borderColorAnimation;

}

- (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"];

    return pulsingLayer;

}

现在就有种渐变的感觉了

4. 同时创建三个扩散动画的CALyer,将开始动画的时间错开,同时添加到animationLayer上。


// 设置静态常量 pulsingCount ,表示 Layer 的数量

static NSInteger const pulsingCount = 3;

// 设置静态常量 animationDuration ,表示动画时间

static double const animationDuration = 3;

- (void)drawRect:(CGRect)rect {


    CALayer *animationLayer = [CALayer layer];


    // 利用 for 循环创建三个动画 Layer

    for (int i = 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;

    return animationGroup;

}

... ...

然后效果有点……一言难尽……

真是很有纪律性的变化啊~~好吧,只需要加入动画曲线就好了

5. 最后加入动画速度曲线


... ...

- (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];

    return animationGroup;

}

... ...

如果需要点扩散,那就设置 frame 极小,同时扩散倍数增大即可。

将动画View垫在另一个圆形View之下即可实现最上方的效果。关闭背景色,重调边框色和边框宽度即可实现第二种效果。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容

  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 2,969评论 0 21
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,326评论 6 30
  • iOS动画篇之CoreAnimation动画 9月 22, 2016发布在Objective-C App如果想被大...
    白水灬煮一切阅读 1,918评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,008评论 5 13
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,004评论 1 23