2010-12-20 63 views
10

我想動畫一個UIToolbar的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 2010-12-20 23:51:51

回答

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

您應該爲動畫使用'CADisplayLink'而不是'NSTimer'。 – rounak 2015-01-14 11:02:59

0

NSTimer,它的設置是一個簡單的動畫了很多工作。這裏是我的解決方案使用派遣,我只是通過改變色彩顏色的Alpha值淡入淡出一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在做任何改變之前。