10

我正在尝试为 UIToolbar 的 tintColor 属性设置动画,以将其从一种 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]; 
4

4 回答 4

4

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

于 2011-01-12T19:39:33.967 回答
2

每个UIView都有一个tintAdjustmentMode

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

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

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

// 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
}
于 2018-05-29T10:44:23.090 回答
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;

}
于 2011-10-25T15:56:46.290 回答
0

NSTimer它的设置对于一个简单的动画来说是很多工作。这是我使用调度的解决方案,我只是UIBarButtonItem通过更改色调颜色的 alpha 值来淡入和淡出:

-(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。

于 2017-03-22T17:32:42.653 回答