我实现了色彩动画。
我想将黑色变色变为渐变色。
我搜索了一些方法,可以使用CAGradientLayer来定义渐变颜色,而CABasicAnimation可以实现颜色更改动画。
但是我在代码下面实现,自我视图不会改变为第一个中的黑色,而且颜色不是动画。
有谁知道这些代码发生了什么?
非常感谢你。 // set black color in the view
CALayer *orginLayer = [CALayer layer];
orginLayer.backgroundColor = [[UIColor blackColor] CGColor];
[self.view.layer addSublayer:orginLayer];
// set gradient layer
CAGradientLayer *colorLayer = [ CAGradientLayer layer];
colorLayer.frame = ( CGRect ){ CGPointZero , CGSizeMake ( self.view.frame.size.width , self.view.frame.size.height )};
colorLayer.position = self .view.center;
// define colors
UIColor *color1 = [UIColor colorWithRed:(255/255.0) green:(137/255.0) blue:(0/255.0) alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:(247/255.0) green:(241/255.0) blue:(107/255.0) alpha:1.0];
NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor,color2.CGColor,nil];
colorLayer.colors = colors;
colorLayer.startPoint = CGPointMake ( 1 , 0 );
colorLayer.endPoint = CGPointMake ( .3, 1 );
// set the color animation black color-> gradient color
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"colors"];
[animation setToValue:colorLayer];
[animation setDuration:10];
[animation setRemovedOnCompletion:YES];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[self.view.layer addAnimation:animation forKey:@"animateGradient"];
答案 0 :(得分:2)
这是你需要做的。也清理了一下你的代码。 :)
// set gradient layer
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = (CGRect){ { }, self.view.frame.size };
colorLayer.position = self.view.center;
// define colors
UIColor *color1 = [UIColor colorWithRed:255/255. green:137/255. blue:0/255. alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:247/255. green:241/255. blue:107/255. alpha:1.0];
NSArray *colors = @[(id)color1.CGColor, (id)color2.CGColor];
colorLayer.colors = colors;
colorLayer.startPoint = CGPointMake(1, 0);
colorLayer.endPoint = CGPointMake(.3, 1);
// set the color animation black color-> gradient color
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"colors"];
animation.fromValue = @[(id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor];
animation.toValue = colors;
animation.duration = 10;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
[colorLayer addAnimation:animation forKey:@"animateGradient"];
[self.view.layer addSublayer:colorLayer];