我可以淡化/动画UIToolbar的tintColor吗?

时间:2010-12-20 23:49:37

标签: iphone ios animation uitoolbar tintcolor

我正在尝试为UIToolbar的tintColor属性设置动画,将其从一个tintColor更改为另一个。

这是我正在尝试的代码。不幸的是,变化立即发生,并且不会从绿色变为蓝色。这很奇怪,因为我知道苹果在连接或打电话时会消失和“脉冲”工具栏色彩。那么为什么这不起作用呢?

// 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]; 

4 个答案:

答案 0 :(得分:3)

通过公共API无法设置的色彩颜色。您可以通过手动更改计时器上的色调颜色来解决此问题。您必须插入中间色彩级别。

答案 1 :(得分: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;

}

答案 2 :(得分:0)

NSTimer它的设置对于一个简单的动画来说是很多工作。这是我使用dispatch的解决方案,我只是通过更改色调颜色的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。

答案 3 :(得分:0)

每个UIView都有tintAdjustmentMode

  

视图中的第一个非默认色调调整模式值   层次结构,从视图本身开始升序和开始。

     

当此属性的值变暗时,将修改tintColor属性的值以提供暗淡的外观。

例如,如果你需要调暗当前的tintColor(暗淡=灰色),只需使用下面的代码:

// Assuming tintAdjustmentMode is set to .automatic
UIView.animate(withDuration: 0.5) {
   yourView.tintAdjustmentMode = .dimmed
}

这会将tintColor设置为灰色并将其调暗。

如果您想将tintColor设置为您自己的自定义颜色:

// Make sure it is set to normal and not automatic
yourView.tintAdjustmentMode = .normal

UIView.animate(withDuration: 0.5) {
   yourView.tintColor = .black
}