动画导航栏标题文本更改 [英] Animate nav bar title text change

查看:104
本文介绍了动画导航栏标题文本更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个页面视图控制器,允许用户在应用程序的不同部分"之间滑动,在导航栏的顶部,将标题文本更改为用户已滑动到的新部分通过pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:.目前,动画制作完成后,它会立即更改标题文本.我想通过一些动画来改善此效果,这是一种微妙的淡入淡出效果.

In my app, I have a page view controller that allows the user to swipe between different "sections" of the app, and at the top in the nav bar I change the title text to the new section the user has swiped to via pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:. It is currently instantly changing the title text when the animation has completed. I would like to improve this with some animation, a subtle fading in and out effect.

我首先尝试实现[UIView animationWithDuration:...]来动画化标题文本的动画,但是它没有动画效果,并且仍然可以即时更新.

I first tried to implement [UIView animationWithDuration:...] to animate changing the title text, but it does not animation and simply still updates instantly.

然后我想知道是否有可能更新导航栏标题的alpha值,因为用户根据他们滚动的距离水平滚动,当下一部分即将出现在屏幕上时达到0 alpha,然后我可以立即更改为0的文本,然后快速淡入为1 Alpha.但是我没有看到UIPageViewControllerDelegate上滚动位置更新后调用的方法.

I then wondered if it'd be possible to update the alpha of the nav bar title as the user scrolls horizontally based on how far they've scrolled, reaching 0 alpha when the next section is about to come on screen, then I can instantly change the text while it's at 0 and then quickly fade in to 1 alpha. But I don't see a method on UIPageViewControllerDelegate that is called when the scroll position has updated.

如果可能的话,不仅可以淡入和淡出,还可以淡化以及在导航栏中移动标题文本的位置,就像通过滑动手势从推键导航中导航时出现的默认动画一样.我将在用户滚动时将旧的节标题滑到另一侧,并在另一侧提供下一个节标题,以便在过渡完成时,上一个节标题不在屏幕上,而新的节标题将居中显示,从而替换完成.但这又需要确切地知道用户滚动了页面视图控制器的多少.

If possible, instead of just fading in and out, I could fade as well as move the title text position in the nav bar like the default animation that occurs when navigating back from a push segue via a swipe gesture. I would slide the old section title over as the user scrolls and provide the next section title on the other side, so that when the transition completes the previous section title is off screen and the new one is perfectly centered so the replacement completes. But again this requires knowing exactly how much the user has scrolled the page view controller.

是否可以实现任何所需的动画?

Is it possible to implement any of the desired animations?

推荐答案

如果要在不同的标题字符串之间设置动画,请使用以下命令:

If you want to animate between different title strings, use the following:

CATransition *fadeTextAnimation = [CATransition animation];
fadeTextAnimation.duration = 0.5;
fadeTextAnimation.type = kCATransitionFade;

[self.navigationController.navigationBar.layer addAnimation: fadeTextAnimation forKey: @"fadeText"];
self.navigationItem.title = "My new title";

当然,您可以调整持续时间并设置合适的计时功能.

You can adjust the duration and set a timing function to suit, of course.

还有其他类型的动画可能会在不同的情况下起作用(感谢@inorganik):

There are also other types of animation that might work in different circumstances (thanks @inorganik):

kCATransitionFade
kCATransitionMoveIn
kCATransitionPush
kCATransitionReveal

这篇关于动画导航栏标题文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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