改变UIViewController的push方式

UIViewController的push默认的是从右往左压入栈,但是有时我们需要其他的方式例如,是从左往右压入栈。还有其他各种方式,如下
方法一:是通过给导航栏下要压入栈的控制器对应的view的layer添加动画

- (IBAction)toPersonalCenterViewControllerAction:(id)sender {
    
UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
PersonalCenterViewController *vc = [board instantiateViewControllerWithIdentifier:@"PersonalCenterViewController"];

    CATransition *transition = [CATransition animation];
    transition.
duration = .5f;
    transition.
timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.
type = kCATransitionPush;push方法
    transition.
subtype = kCATransitionFromLeft;从左到右
    transition.
delegate = self;
    [
self.controller.navigationController.view.layer addAnimation:transition forKey:nil];

    [
self.controller.navigationController pushViewController:vc animated:YES];
}
 对应的动画效果,还有对应的动画方向如下
transition.type 

/* Common transition types. */

CA_EXTERN NSString * const kCATransitionFade
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionMoveIn
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionPush
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionReveal
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

 
transition.subtype
/* Common transition subtypes. */

CA_EXTERN NSString * const kCATransitionFromRight
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromLeft
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromTop
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromBottom
    
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 
方法二:通过自定义navigationController
#import <UIKit/UIKit.h>
@interface CustomViewController : UINavigationController
@end
 
@implementation CustomViewController
 
-(UIViewController * )popViewControllerAnimated:(BOOL)animated
{
    if (animated) {
        [UIViewbeginAnimations:@"1"context:nil];
        [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
        [UIViewsetAnimationDuration:1];
        [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:NO];
        [UIViewcommitAnimations];
       
    }
    return [superpopViewControllerAnimated:animated];
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (animated) {
        [UIViewbeginAnimations:@"2"context:nil];
        [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
        [UIViewsetAnimationDuration:2];
        [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.viewcache:NO];
        [UIViewcommitAnimations];
    }
         [super pushViewController:viewController animated:animated];
}
@end
原文地址:https://www.cnblogs.com/wuxiufang/p/3582784.html