2013-02-17 8 views
12

私はこの質問を以前に聞いたことがありますが、私はあなたの助けを借りて何をしたいのかを明確にするためにもう一度お伝えしたいと思います。私はあなたが太陽の天気アプリ(スクリーンショットが提供されている)の背景に似てxCodeのバックグラウンドとiOSアプリケーションをどのように作成するのかと思っていました。あなたが見ることができるように、勾配は非常に滑らかであり、明らかに2つ以上の主要な点を含んでいる。 コードの例やスニペットに関する助けをいただければ幸いです。ありがとう、ベン。xCodeのSolar Weather Applicationに似たアニメーショングラデーションの背景を作成する

Screenshot 1 Screenshot 2

答えて

16

何が必要なのである。

  1. 追加CAGradientLayer例えば、あなたのビューコントローラ(またはカスタムビュー)、へ:あなたのビューコントローラで

    @property (nonatomic, retain) CAGradientLayer *gradient; 
    
  2. 、viewDidLoadで、グラデーションレイヤーを作成してビューに追加します。

    self.gradient = [CAGradientLayer layer]; 
    gradient.frame = self.view.bounds; 
    gradient.colors = [NSArray arrayWithObjects: 
           (id)topColor.CGColor, 
           (id)bottomColor.CGColor, 
           nil]; 
    gradient.locations = [NSArray arrayWithObjects: 
            [NSNumber numberWithFloat:0.0f], 
            [NSNumber numberWithFloat:0.7], 
            nil]; 
    [self.view.layer addSublayer:self.gradient]; 
    

    3a。実行して、適切な間隔でself.gradientを更新しますあなたのコントローラにNSTimerを追加します。

    topColor = ...; bottomColor = ...; 
        NSArray* newColors = [NSArray arrayWithObjects: 
           (id)topColor.CGColor, 
           (id)bottomColor.CGColor, 
           nil]; 
    [(CAGradientLayer *)self.layer setColors:newColors]; 
    

    これは、あなたが正確にあなたが各ステップで勾配のために使用したい色を決定することができます。それ以外の場合は、次のようにアニメーションを試してみてください。

    3bあなたはまた、3a、3bとを組み合わせかもしれません

    - (void)animateLayer... { 
    
        [UIView animateWithDuration:duration 
             delay:0 
             options:UIViewAnimationOptionCurveEaseInOut 
            animations:^{ 
        [CATransaction begin]; 
        [CATransaction setAnimationDuration:duration]; 
    
         topColor = ...; bottomColor = ...; 
         NSArray* newColors = [NSArray arrayWithObjects: 
           (id)topColor.CGColor, 
           (id)bottomColor.CGColor, 
           nil]; 
        [(CAGradientLayer *)self.layer setColors:newColors]; 
    
        [CATransaction commit]; 
        } 
          completion:^(BOOL b) { 
           [self animateLayer..]; 
          }]; 
    } 
    

、このようなアニメーションを追加します。

+0

どのようにして3bが実装されますか? –

+0

3bは既に指定された期間のアニメーションを提供しています。あなたはそれをメソッドにパックして呼び出すだけです(つまりviewDidLoadから)。 'duration'秒後に繰り返すような仕組みを提供する必要があります。そのためにはタイマーを使うか、アニメーションの 'completion'ブロックを使うことができます。私はこれを考慮に入れて私の答えを広げました。 – sergio

+1

このコードは、いくつかのエラーが発生したとして実装されています:宣言されていない識別子topColorの使用。プロパティ 'bounds'がオブジェクトタイプ "View Controller *"に見つかりません。 –

13

簡単な作業例:

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

あなたは変化を生き返らその後、色をシフトし、するための追加メソッドを実装する場合があります。しかし、これはあなたがそこに着くのを助けるはずです。

+0

私はこれを迅速に適用することができましたが、アニメーションが完了すると最初の色に戻ります。この行をswift [self.gradient setColors:toColors]に変換するにはどうすればよいですか?ありがとう – DrPatience

+2

シンプル: 'self.gradient.colors = toColors' – TheTiger

+0

あなたの' viewDidAppear'に 'super'を呼び出すことも良い習慣です。 – smaili

関連する問題