2010-12-20 83 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在做任何改变之前。