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

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

你可能感兴趣的:(iOS 动画之光条滑动)