在名为“Luvocracy”的应用程序中,当用户在屏幕上向上滑动时,导航栏的标题会更改。旧标题被推高了,而新标题被过渡了。我现在没有视频,但是这里有一些屏幕截图:
https://www.dropbox.com/s/sns0bsxkdv7pw3l/Photo%20Apr%2008%2C%2011%2001%2005%20AM.png
https://www.dropbox.com/s/ys9a49u3dyxrlcm/Photo%20Apr%2008%2C%2011%2001%2009%20AM.png
https://www.dropbox.com/s/dlcfvfvqqov3ag7/Photo%20Apr%2008%2C%2011%2001%2013%20AM.png
如何显示新的导航栏标题中的动画或过渡?
编辑:该应用程序不再在应用程序商店中提供,因此我无法上传此操作的视频。
最佳答案
您可以使用CATransition为更改的标题设置动画...但是,由于标题本身是导航栏上的私有(private)属性,因此需要首先创建一个自定义标签并将其附加到导航项上。
设置标题标签(这将覆盖默认导航栏的标题):
UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f) /* auto-sized anyway */];
titleLabelView.backgroundColor = [UIColor clearColor];
titleLabelView.textAlignment = NSTextAlignmentCenter;
titleLabelView.textColor = [UIColor blackColor];
titleLabelView.font = [UIFont systemFontOfSize:16.0f];
titleLabelView.adjustsFontSizeToFitWidth = YES;
titleLabelView.text = @"@cracy123";
self.navigationItem.titleView = titleLabelView;
然后,每当您要为更改的标题制作动画(假设滚动 View 委托(delegate) Action )时,添加一个CAAnimation图层并保存:
CATransition *animation = [CATransition animation];
animation.duration = 1.0;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.navigationItem.titleView.layer addAnimation:animation forKey:@"changeTitle"];
((UILabel*)self.navigationItem.titleView).text = @"JACOB K";
显然,您可以更改CATransition动画属性以获得所需的效果,但是这些将为您提供“推上”效果。