UINavigationBar 使用动画更改色调颜色

发布于 2024-10-18 04:59:37 字数 308 浏览 1 评论 0原文

是否可以通过动画更改色调以获得更平滑的效果?

这对我不起作用:

[UIView beginAnimations:nil context:nil];
[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
[UIView commitAnimations];

我不确定是否可以使用原生苹果组件,因为我猜他们会使用 CG 来生成渐变......只是想在开始构建自己的解决方案之前找出答案...

干杯伙计们:)

Is it possible to change the tint with animation for a smoother effect?

This doesn't work for me:

[UIView beginAnimations:nil context:nil];
[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
[UIView commitAnimations];

I a not sure if it is even possible using native apple components as I guess they will be using CG to generate the gradient ... just want to find out before I'll start building my own solution ...

Cheers guys :)

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

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

发布评论

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

评论(3

霓裳挽歌倾城醉 2024-10-25 04:59:37

你不会相信我花了这么长时间才真正实现了这一目标;这让我非常恼火,因为这不是 iOS 的原生功能,而且tintColor 的过渡看起来很难看,而推送/弹出 viewController 的动画却如此流畅。

有很多代码用于检查何时淡入淡出,我什至编写了一个名为 PSPDFNavigationAppearanceSnapshot 的类来在弹出时保留导航状态。 (我从很棒的 NimbusKit 那里得到了这个想法)

实际的动画非常简单:

[self.navigationController.navigationBar.layer addAnimation:PSPDFFadeTransition() forKey:nil];

CATransition *PSPDFFadeTransition(void) {
    return PSPDFFadeTransitionWithDuration(0.25f);
}

CATransition *PSPDFFadeTransitionWithDuration(CGFloat duration) {
    CATransition *transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.duration = duration;
    return transition;
}

你可以压缩它编写更多代码;它是从我的 iOS PDF 库 PSPDFKit 中截取的,我在各个地方使用了淡入淡出,因此使用了辅助函数。

You wouldn't believe the length I went through to actually make that possible; it annoyed me to no end that this isn't a stock iOS feature and that transition of tintColor look ugly while the animation to push/pop a viewController is so smooth.

There'a lot of code that checks when to fade, and I've even written a class called PSPDFNavigationAppearanceSnapshot to preserve the navigation state when being popped. (I got that idea from the awesome NimbusKit)

The actual animation is pretty easy:

[self.navigationController.navigationBar.layer addAnimation:PSPDFFadeTransition() forKey:nil];

CATransition *PSPDFFadeTransition(void) {
    return PSPDFFadeTransitionWithDuration(0.25f);
}

CATransition *PSPDFFadeTransitionWithDuration(CGFloat duration) {
    CATransition *transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.duration = duration;
    return transition;
}

You can compact that code even more; it's a snipped from my iOS PDF library PSPDFKit and I use the fade in various places, thus the helper functions.

濫情▎り 2024-10-25 04:59:37

您无法对条形色调进行动画处理 - 可以通过这种方式进行动画处理的属性列表(对于 UIView)是 此处

我认为你根本无法做到这一点,除非你想用背景颜色正在变化的 UIView 覆盖一个具有中性色调的栏。 backgroundColor 是可以设置动画的属性之一。但是如果你想将 UIView 放在导航栏的顶部,你可能必须偷偷摸摸,我不知道有什么方法可以做到这一点。

另一个想法 - 子类化并在drawRect中自己绘制?

You can't animate bar tint - the list of properties (for a UIView) that can be animated in this way is here.

I don't think you can do that at all unless you want to overlay a bar that has a neutral tint with a UIView whose background color is changing. backgroundColor is one of the properties that can be animated. But you may have to get sneaky if you want to put a UIView on top of the navigation bar, I don't know of a way to do that.

Another thought - subclassing and doing the drawing yourself in drawRect?

仲春光 2024-10-25 04:59:37

我们在另一个线程中找到了解决方案: Transition Color navBar

解决方案是对属性 barTinTColor 进行动画处理。这是代码:

 self.navigationController.navigationBar.barTintColor = [UIColor redColor];
 [UIView animateWithDuration:5.0f animations:^{
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
} completion:^(BOOL finished) {
}];

We find the solution in another thread : Transition Color navBar

The solution was animating the attribut barTinTColor. Here is the code :

 self.navigationController.navigationBar.barTintColor = [UIColor redColor];
 [UIView animateWithDuration:5.0f animations:^{
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
} completion:^(BOOL finished) {
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文