Animating UINavigationController's 'back' button

我是研究僧i 提交于 2019-12-03 05:06:38

I've done something similar to what you describe, i.e. change the default animation for pop and push of view in UINavigationController.

The idea is to disable the default animation for the object and replace it with your own animation. I've created a new category for UINavigationController and used a function similar to the one below.

 - (void) altAnimatePopViewControllerAnimated:(BOOL)animated
 {
[CATransaction begin];

CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionPush;          // Use any animation type and subtype you like
transition.subtype = kCATransitionFromTop;
transition.duration = 0.3;

CATransition *fadeTrans = [CATransition animation];
fadeTrans.type = kCATransitionFade;
fadeTrans.duration = 0.3;


[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];

[[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
[[[[self.view subviews] objectAtIndex:1] layer] addAnimation:fadeTrans forKey:nil];



[self  popViewControllerAnimated:YES];
[CATransaction commit];
   }

to use just use the code

   [self.navigationController altAnimatePopViewControllerAnimated:YES];

In order to do a push just create another similar function and reverse the animation.

It's not perfect but it works. When you choose different animation types play around with the different subviews composing the navigation bar/controller to get it perfect.

Took my quite a white to come up with this, so use it wisely :)

******* EDIT

Try replacing the push transition with this (Didn't try it myself):

  CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
  scale.duration = duration;
  scale.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:.5f],
              [NSNumber numberWithFloat:1.2f],
              [NSNumber numberWithFloat:.85f],
              [NSNumber numberWithFloat:1.f],
              nil];

This will do a pop in, i.e. the view will be bigger before it settles to the right size. Play with the values in the array to control the curve of the animation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!