簡體   English   中英

iOS CABasicAnimation顏色動畫不起作用

[英]iOS the CABasicAnimation color animation not effect

我實現了彩色動畫。

我想將黑色變成漸變色。

我搜索了一些方法,可以使用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"];

這是您需要做的。 也清理了一下代碼。 :)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM