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

发布于 2024-10-08 05:36:44 字数 621 浏览 11 评论 0原文

我正在尝试对 UIToolbar 的tintColor 属性进行动画处理,将其从一种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]; 

I am trying to animate a UIToolbar's tintColor property, to change it from one tintColor to another.

Here is the code I am trying. Unfortunately, the change occurs immediately and does not fade from green to blue. This is strange because I know Apple fades and "pulses" toolbar tint colors when tethering or on a phone call. So why doesn't this work?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断爱 2024-10-15 05:36:44

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

The tint color in not animatable through public APIs. You can work around this by manually changing the tint color on a timer. You would have to interpolate the intermediate color levels.

七度光 2024-10-15 05:36:44

每个 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
}

Each UIView has a tintAdjustmentMode:

The first non-default tint adjustment mode value in the view’s
hierarchy, ascending from and starting with the view itself.

When this property’s value is dimmed, the value of the tintColor property is modified to provide a dimmed appearance.

For example, if you need to dim the current tintColor (dim = grey color) just use the code below:

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

This will animate the tintColor to grey and will dim it.

If you want to animate the tintColor to your own custom color:

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

UIView.animate(withDuration: 0.5) {
   yourView.tintColor = .black
}
此刻的回忆 2024-10-15 05:36:44

为了供将来参考,这是我有点仓促的动画色调解决方案。我确信有更聪明的方法可以通过类别和 RGB 值的结构来做到这一点。我很着急好吗?!

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;

}

For future reference, here's my somewhat hasty solution for animating tint. I'm sure there are more clever ways of doing this with categories and a struct for the rgb values yadda yadda. I was in a hurry, OK?!

UITabBarController subclass:

@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;

}
南七夏 2024-10-15 05:36:44

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在进行任何更改之前。

NSTimer and it's setup is a lot of work for a simple animation. Here's my solution using dispatch, I'm just fading a UIBarButtonItem in and out by changing the alpha value of the tint color:

-(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);
}

The linear curve can be a little noticeable but I'm interested in trying CADisplayLink first before making any changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文