码迷,mamicode.com
首页 > 其他好文 > 详细

OC UINavigationController push pop自定义动画

时间:2017-08-07 15:13:21      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:getheight   btn   ram   navig   one   技术分享   miss   动画   atomic   

 技术分享

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];

}

 

OC UINavigationController push pop自定义动画

标签:getheight   btn   ram   navig   one   技术分享   miss   动画   atomic   

原文地址:http://www.cnblogs.com/hualuoshuijia/p/7298987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!