当ios遇上css3动画系列(2) --ios动画之flash 闪烁

当ios遇上css3动画系列(2) --ios动画之flash 闪烁

这篇文章来看下flash闪烁动画的。
上css3动画代码

@-webkit-keyframes flash {
  0%, 50%, 100% {
    opacity: 1;
  }

  25%, 75% {
    opacity: 0;
  }
}

依据css3代码,写出ios动画代码

- (void)flash{
    CAKeyframeAnimation* keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    float opacity_1 = 1;
    float opacity_2 = 0;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[self getNumberWithFloat:opacity_1]];
    [values addObject:[self getNumberWithFloat:opacity_2]];
    [values addObject:[self getNumberWithFloat:opacity_1]];
    [values addObject:[self getNumberWithFloat:opacity_2]];
    [values addObject:[self getNumberWithFloat:opacity_1]];
    [keyAnim setValues:values];
    
    NSMutableArray *keyTimes = [NSMutableArray array];
    [keyTimes addObject:[self getNumberWithFloat:0]];
    [keyTimes addObject:[self getNumberWithFloat:0.25]];
    [keyTimes addObject:[self getNumberWithFloat:0.5]];
    [keyTimes addObject:[self getNumberWithFloat:0.75]];
    [keyTimes addObject:[self getNumberWithFloat:1.0]];
    [keyAnim setKeyTimes:keyTimes];
    [keyAnim setDuration:1.0];
        [keyAnim setFillMode:kCAFillModeBoth];
    [self.TextTest.layer addAnimation:keyAnim forKey:nil];
    
}

gif效果图


使用了CABasicAnimation 动画来做,比较简单,opacity值的变化。

-(void)breathingLight{
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 1;
    animation.repeatCount = HUGE_VALF;
    animation.fromValue= [NSNumber numberWithFloat:1];
    animation.toValue = [NSNumber numberWithFloat:0.2];
    animation.autoreverses = YES;
    [self.TextTest.layer addAnimation:animation forKey:nil];
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,680评论 7 249
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,713评论 6 30
  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 5,564评论 0 7
  • 六:理解对“属性”这一概念 可以用@property语法来定义对象中的所封装的数据通过"特质"来制定存储数据所需的...
    苏苏咯阅读 1,033评论 0 0
  • 给词写话,课文集美,信息提取, 句式学用,段式学用,篇式仿写, 生动译写,想象扩写,片段点评, 诗联欣赏,课文概述...
    春光里的小米阅读 1,664评论 0 0