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 th
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.
There are also other types of animation that might work in different circumstances (thanks @inorganik):
kCATransitionFade
kCATransitionMoveIn
kCATransitionPush
kCATransitionReveal
A solution is to create a custom title and animate its position using the page view controller's hidden scrollView's delegate method. As Zhang stated, the custom title is simply self.navigationItem.titleView = customNavTitleLabel;
If you need to animate only title (not the whole nav bar)
Develop custom title control Link for example
Assign
let titleView = UIAnimatedTitleView(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
titleView.text = "Hello"
Animate
var flag = true
@objc private func animateNavigationTitle() {
guard let titleView = navigationItem.titleView as? UIAnimatedTitleView else { return }
let fadeTextAnimation = CATransition()
fadeTextAnimation.duration = 0.5
fadeTextAnimation.type = kCATransitionPush
fadeTextAnimation.subtype = kCATransitionFromTop
titleView.layer.add(fadeTextAnimation, forKey: "pushText")
titleView.text = flag ? "Hello" : "Good buy"
flag = !flag
}
Found the best way is this category:
@implementation UIViewController (ControllerNavigationEffects)
-(void) setNavigationTitleWithAnimation:(NSString *) title {
if ([self.navigationItem.title isEqualToString:title]) {
return;
}
@weakify(self);
float duration = 0.2;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
@strongify(self);
self.navigationItem.titleView.alpha = 0;
} completion:^(BOOL finished) {}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self);
self.navigationItem.titleView = nil;
self.navigationItem.title = title;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.navigationItem.titleView.alpha = 1;
} completion:nil];
});
}
-(void) setNavigationTitleViewWithAnimation:(UIView *) titleView {
if ([self.navigationItem.titleView isKindOfClass:[titleView class]]) {
return;
}
@weakify(self);
float duration = 0.2;
CATransition *fadeTextAnimation = [CATransition animation];
fadeTextAnimation.duration = duration;
fadeTextAnimation.type = kCATransitionFade;
[self.navigationController.navigationBar.layer addAnimation: fadeTextAnimation forKey: @"fadeText"];
self.navigationItem.title = @"";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self);
self.navigationItem.title = @"";
self.navigationItem.titleView = titleView;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.navigationItem.titleView.alpha = 1;
} completion:nil];
});
}
let fadeTextAnimation = CATransition()
fadeTextAnimation.duration = 0.5
fadeTextAnimation.type = .fade
navigationController?.navigationBar.layer.add(fadeTextAnimation, forKey: "fadeText")
navigationItem.title = "test 123"
For convenience, Ashley Mills solution in Swift:
11/16/2016 Updated for Swift 3 (Thanks to n13)
let fadeTextAnimation = CATransition()
fadeTextAnimation.duration = 0.5
fadeTextAnimation.type = kCATransitionFade
navigationController?.navigationBar.layer.add(fadeTextAnimation, forKey: "fadeText")
navigationItem.title = "test 123"
Swift 2.x
let fadeTextAnimation = CATransition()
fadeTextAnimation.duration = 0.5
fadeTextAnimation.type = kCATransitionFade
navigationController?.navigationBar.layer.addAnimation(fadeTextAnimation, forKey: "fadeText")
navigationItem.title = "test 123"
I tip my hat to Ashley!