1. DSLTransitionFromFirstToSecond
#import <UIKit/UIKit.h> @interface DSLTransitionFromFirstToSecond : NSObject<UIViewControllerAnimatedTransitioning> /** isPresenting是否是pop动画, */ @property (nonatomic, assign) BOOL isPresenting; @end
#import "DSLTransitionFromFirstToSecond.h" @implementation DSLTransitionFromFirstToSecond - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext { return 3; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { /** * dismissing */ //isPresenting如果为YES就是pop动画,为NO就是 if (self.isPresenting) { [self executeDismissAnimation:transitionContext]; } else { [self executePresentationAnimation:transitionContext]; } } /** * present 动画 * * @param transitionContext */ - (void)executePresentationAnimation:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *containerView = [[UIApplication sharedApplication] keyWindow]; CGRect frame = containerView.bounds; frame.origin.y = -CGRectGetHeight(frame); toViewController.view.frame = frame; [containerView addSubview:toViewController.view]; NSTimeInterval duration = [self transitionDuration:transitionContext]; frame.origin.y = 0; [UIView animateWithDuration:duration animations:^{ toViewController.view.frame = frame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } /** * dismiss 动画 * * @param transitionContext */ - (void)executeDismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *containerView = [[UIApplication sharedApplication] keyWindow]; CGRect frame = containerView.bounds; frame.origin.y = - CGRectGetHeight(frame); toViewController.view.userInteractionEnabled = YES; [transitionContext.containerView addSubview:toViewController.view]; [transitionContext.containerView addSubview:fromViewController.view]; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; fromViewController.view.frame = frame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; }
viewcontorller:代码
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"==============="); // 让我自己变成navigationController的delegate self.navigationController.delegate = self; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // 我不再是 navigationController 的代理啦 if (self.navigationController.delegate == self) { self.navigationController.delegate = nil; ; } } - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { DSLTransitionFromFirstToSecond *second =[[DSLTransitionFromFirstToSecond alloc] init]; second.isPresenting = NO; return second; } - (IBAction)alertBtnClick:(UIButton *)sender { [self.navigationController pushViewController:[FirstViewController new] animated:YES]; }
FirstViewController:
-(DSLTransitionFromFirstToSecond *)second{ if(!_second) { _second = [[DSLTransitionFromFirstToSecond alloc]init]; _second.isPresenting = YES; } return _second; } - (id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { /** * typedef NS_ENUM(NSInteger, UINavigationControllerOperation) { * UINavigationControllerOperationNone, * UINavigationControllerOperationPush, * UINavigationControllerOperationPop, * }; */ //push的时候用我们自己定义的customPush if (operation == UINavigationControllerOperationPop ) { NSLog(@"========"); return self.second; }else{ return nil; } } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.navigationController.delegate = self; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; [self.view addSubview:btn]; btn.backgroundColor = [UIColor yellowColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; } -(void)btnClick{ [self.navigationController popViewControllerAnimated:YES]; }
时间: 2024-11-05 06:12:17