UINavigationBar 用动画改变 Tint 颜色 [英] UINavigationBar change Tint color with animation

查看:25
本文介绍了UINavigationBar 用动画改变 Tint 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这对我不起作用:

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

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

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 ...

伙计们干杯:)

推荐答案

你不会相信我花了这么多时间才真正做到这一点;这让我很恼火,因为这不是 iOS 的常用功能,而且 tintColor 的过渡看起来很难看,而推动/弹出 viewController 的动画非常流畅.

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.

有很多代码检查 when 是否消失,我什至编写了一个名为 PSPDFNavigationAppearanceSnapshot 的类来在弹出时保留导航状态.(我从很棒的 NimbusKit 得到这个想法)

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)

实际的动画很简单:

[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 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.

这篇关于UINavigationBar 用动画改变 Tint 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆