到目前为止,这是我的代码:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = twoDrums.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.00], [NSNumber numberWithFloat:0.70] , nil];
[twoDrums.layer insertSublayer:gradient atIndex:0];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
animation.fromValue = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
animation.toValue = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
animation.duration = 15.0;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[gradient addAnimation:animation forKey:@"animateGradient"];
这是一个渐变,从红色/黑色变为蓝色/黑色再向后变化。我想做的是在同一个动画中从红色/黑色变为黄色/黑色到蓝色/黑色再到绿色/黑色等。
非常感谢任何帮助。谢谢,圣诞快乐!
答案 0 :(得分:1)
我知道你要求多个动画,但这就是我最后只用一个渐变色变化的方法。也许你可以从中构建。
首先我像这样制作渐变层:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = _view.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor yellowColor] CGColor], (id)[[UIColor orangeColor] CGColor], nil];
UIView *gradientView = [[UIView alloc] initWithFrame:_view.frame];
[gradientView setBackgroundColor:[UIColor clearColor]];
[gradientView.layer insertSublayer:gradient atIndex:0];
[gradientView setHidden:NO];
[_window addSubview:gradientView];
然后我设置颜色变化的动画:
- (void)animateViewToTransparent:(UIView *)view {
NSArray *sublayers = [view.layer sublayers];
CAGradientLayer *gradientLayer = [sublayers objectAtIndex:0];
CABasicAnimation *gradientAnimation = [CABasicAnimation animationWithKeyPath:@"colors"];
gradientAnimation.toValue = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], [[UIColor clearColor] CGColor], nil];
gradientAnimation.duration = 0.4f;
URAnimationDelegate *delegate = [[URAnimationDelegate alloc] init];
[gradientAnimation setDelegate:delegate];
[gradientLayer addAnimation:gradientAnimation forKey:@"colors"];
}
现在动画将起作用,但动画完成后颜色变化将不会停留。 URAnimationDelegate类的最后一步是设置layer.colors属性。
NSArray *sublayers = [_view.layer sublayers];
CAGradientLayer *gradientLayer = [sublayers objectAtIndex:0];
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], [[UIColor clearColor] CGColor], nil];
Etvoilà!
答案 1 :(得分:0)
使用CAAnimationGroup结束运行。因为每个动画都必须设置一个beginTime,所以它们不会同时启动。