iOS 动画之光条滑动

项目中需要实现一个打光的动画效果,先看下效果图

图像.gif

录的视频转成gif图,不是很清晰。效果在21点跟快速开始图片上。

实现代码如下:

//动效 21 dian
UIImageView *twoOneImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, gameSize.width, gameSize.height)];
twoOneImageView.image = ImageNamed(gameImageName);
[_gameButton addSubview:twoOneImageView];
// 初始化渐变层
self.twoOneRotColorLayer = [CAGradientLayer layer];
self.twoOneRotColorLayer.frame = twoOneImageView.frame;
self.twoOneRotColorLayer.position = twoOneImageView.center;
[_gameButton.layer addSublayer:self.twoOneRotColorLayer];

//颜色数组
self.twoOneRotColorLayer.colors = @[(__bridge id)[kColorffffff colorWithAlphaComponent:0.1].CGColor,(__bridge id)[kColorffffff colorWithAlphaComponent:0.3].CGColor,(__bridge id)[kColorffffff colorWithAlphaComponent:0.1].CGColor];
//颜色分层
self.twoOneRotColorLayer.locations = @[@(- 0.2),@(- 0.1),@(0)];
// 渐变方向
self.twoOneRotColorLayer.startPoint = CGPointMake(0, 0.1);
self.twoOneRotColorLayer.endPoint = CGPointMake(1, 0.9);
self.twoOneRotColorLayer.mask = twoOneImageView.layer;

//动效 kuaisukaishi
UIImageView *quickStartImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, taskSize.width, taskSize.height)];
quickStartImageView.image = ImageNamed(taskImageName);
[_taskButton addSubview:quickStartImageView];
// 初始化渐变层
self.quickStartColorLayer = [CAGradientLayer layer];
self.quickStartColorLayer.frame = quickStartImageView.frame;
self.quickStartColorLayer.position = quickStartImageView.center;
[_taskButton.layer addSublayer:self.quickStartColorLayer];

//颜色数组
self.quickStartColorLayer.colors = @[(__bridge id)[kColorffffff colorWithAlphaComponent:0.1].CGColor,(__bridge id)[kColorffffff colorWithAlphaComponent:0.3].CGColor,(__bridge id)[kColorffffff colorWithAlphaComponent:0.1].CGColor];
//颜色分层
self.quickStartColorLayer.locations = @[@(- 0.2),@(- 0.1),@(0)];
// 渐变方向
self.quickStartColorLayer.startPoint = CGPointMake(0, 0.9);
self.quickStartColorLayer.endPoint = CGPointMake(1, 0.1);
self.quickStartColorLayer.mask = quickStartImageView.layer;


self.locationAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
self.locationAnimation.fromValue = @[@(-0.2), @(-0.1),@(0)] ;
self.locationAnimation.toValue = @[@(1.0),@(1.1),@(1.2)] ;
self.locationAnimation.duration = 2.0 ;

self.colorTimer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(gradientLayerEvent) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.colorTimer forMode:NSDefaultRunLoopMode];


#pragma mark --- 定时器事件
-(void)gradientLayerEvent{
   // 动效
   if (self.locationAnimation) {
      [self.twoOneRotColorLayer addAnimation:self.locationAnimation forKey:nil];
      [self.quickStartColorLayer addAnimation:self.locationAnimation forKey:nil];
  }
}

个人觉得写的不是很好,希望大佬指出不足之处。以供学习交流。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容