簡単な作業例:
.hファイル
@property (strong, nonatomic) CAGradientLayer *gradient;
.mファイル
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
self.gradient = [CAGradientLayer layer];
self.gradient.frame = self.view.bounds;
self.gradient.colors = @[(id)[UIColor purpleColor].CGColor,
(id)[UIColor redColor].CGColor];
[self.view.layer insertSublayer:self.gradient atIndex:0];
[self animateLayer];
}
-(void)animateLayer
{
NSArray *fromColors = self.gradient.colors;
NSArray *toColors = @[(id)[UIColor redColor].CGColor,
(id)[UIColor orangeColor].CGColor];
[self.gradient setColors:toColors];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
animation.fromValue = fromColors;
animation.toValue = toColors;
animation.duration = 3.00;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.delegate = self;
// Add the animation to our layer
[self.gradient addAnimation:animation forKey:@"animateGradient"];
}
あなたは変化を生き返らその後、色をシフトし、するための追加メソッドを実装する場合があります。しかし、これはあなたがそこに着くのを助けるはずです。
どのようにして3bが実装されますか? –
3bは既に指定された期間のアニメーションを提供しています。あなたはそれをメソッドにパックして呼び出すだけです(つまりviewDidLoadから)。 'duration'秒後に繰り返すような仕組みを提供する必要があります。そのためにはタイマーを使うか、アニメーションの 'completion'ブロックを使うことができます。私はこれを考慮に入れて私の答えを広げました。 – sergio
このコードは、いくつかのエラーが発生したとして実装されています:宣言されていない識別子topColorの使用。プロパティ 'bounds'がオブジェクトタイプ "View Controller *"に見つかりません。 –