UINavigationBar 使用动画更改色调颜色
是否可以通过动画更改色调以获得更平滑的效果?
这对我不起作用:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不会相信我花了这么长时间才真正实现了这一目标;这让我非常恼火,因为这不是 iOS 的原生功能,而且tintColor 的过渡看起来很难看,而推送/弹出 viewController 的动画却如此流畅。
有很多代码用于检查何时淡入淡出,我什至编写了一个名为 PSPDFNavigationAppearanceSnapshot 的类来在弹出时保留导航状态。 (我从很棒的 NimbusKit 那里得到了这个想法)
实际的动画非常简单:
你可以压缩它编写更多代码;它是从我的 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:
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.
您无法对条形色调进行动画处理 - 可以通过这种方式进行动画处理的属性列表(对于 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?
我们在另一个线程中找到了解决方案: Transition Color navBar
解决方案是对属性 barTinTColor 进行动画处理。这是代码:
We find the solution in another thread : Transition Color navBar
The solution was animating the attribut barTinTColor. Here is the code :