2010-12-20 11 views
10

UIToolbarのtintColorプロパティをアニメーション化して、あるtintColorから別のtintColorに変更しようとしています。UIToolbarのtintColorをフェード/アニメーションできますか?

ここに私が試しているコードがあります。残念ながら、この変化は直ちに起こり、緑から青に変わらない。これは奇妙です。なぜなら、Appleがテザリングや通話時にツールバーの色合いを「脈打つ」ように消えることを知っているからです。ではなぜこれは機能しませんか?

// set initial tint color 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.95 blue:0.15 alpha:0.6]; 

//animation stuff 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.95]; 
[UIView setAnimationDelegate:self];   

//thing to animate 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.35 blue:0.45 alpha:0.6]; 

//animation stuff 
[UIView commitAnimations]; 
+0

WSYIWYGエディタでバイナリボタンを使用してコードを貼り付けてください。そうすることで、その書式が維持されます。 – TALLBOY

答えて

3

色合いは、公開APIでアニメーション化できません。これを回避するには、タイマーの色合いを手動で変更します。中間色レベルを補間する必要があります。

0

今後の参考に、色合いをアニメーション化するための私のやや早い解決策があります。私はカテゴリとrgb値yadda yaddaのための構造体でこれを行うもっと巧妙な方法があると確信しています。私は急いでいました。

UITabBarControllerサブクラス:

@interface BaseNavigationController : UINavigationController 
{ 
    NSTimer *   tintTimer; 
    UIColor *   targetColor; 
} 

-(void)changeTintTo:(UIColor*)color; 
-(void)animateTint; 

-(void)changeTintTo:(UIColor*)color; 
{ 
    targetColor = [color retain]; 

    [tintTimer invalidate]; 
    tintTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(animateTint) userInfo:nil repeats:YES]; 

} 

-(void)animateTint; 
{ 
    UIColor * currentColor = self.navigationBar.tintColor; 

    CGFloat red, green, blue, alpha; 
    [currentColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    CGFloat targetRed, targetGreen, targetBlue, targetAlpha; 
    [targetColor getRed:&targetRed green:&targetGreen blue:&targetBlue alpha:&targetAlpha]; 

    CGFloat newRed = red + ((targetRed - red)*.2); 

    UIColor * newColor = [UIColor colorWithRed:newRed 
             green:green + ((targetGreen - green)*.2) 
              blue:blue + ((targetBlue - blue)*.2) // the .2 adjusts the fade speed 
             alpha:1.0]; 

    if(
     (newRed < targetRed && newRed >= targetRed-0.01) || 
     (newRed > targetRed && newRed <= targetRed+0.01) 
    ) 
    { 
     newColor = targetColor; 
     [targetColor autorelease]; 
     [tintTimer invalidate]; 
     tintTimer = nil; 
     targetColor = nil; 
    } 

    self.navigationBar.tintColor = newColor; 

} 
+0

アニメーションには 'NSTimer'ではなく' CADisplayLink'を使うべきです。 – rounak

0

NSTimer簡単なアニメーションのために多くの作業があります。ここに私の解決策は、ディスパッチを使用してだ、私はちょうど色合いの色のアルファ値を変更することにより、内と外UIBarButtonItemフェージングだ:

-(void)animateItemToTargetAlpha:(CGFloat)targetAlpha 
{ 
    static dispatch_source_t timer = nil; 
    static CGFloat DURATION = 0.25f; 
    static CGFloat FPS = 30.f; 
dispatch_source_cancel(timer); 
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1/FPS * NSEC_PER_SEC,(1ull * NSEC_PER_SEC)/10); 

    CGFloat red, green, blue, __block alpha; 
    [self.navigationItem.rightBarButtonItem.tintColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    dispatch_source_set_event_handler(timer, ^{ 
     alpha = targetAlpha == 1.0f ? alpha + (1/(FPS * DURATION)) : alpha - (1/(FPS * DURATION)); 
     self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
     if(alpha >= 1 || alpha <= 0) 
     { 
      dispatch_source_cancel(timer); 
     } 
    }); 

    dispatch_resume(timer); 
} 

線形曲線が少し目立つことができますが、私は最初CADisplayLinkを試みることに興味があります変更を加える前に

関連する問題