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

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

问题描述

在我的应用程序中,我有一个页面视图控制器,允许用户在应用程序的不同部分"之间滑动,在导航栏的顶部,我将标题文本更改为用户滑动到的新部分通过 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天全站免登陆