使用present实现push动画效果

//

//  HYBModalTransition.h

//  PresentDismissTransitionDemo

//

//  Created by huangyibiao on 15/12/21.

//  Copyright © 2015 huangyibiao. All rights reserved.

//

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, HYBModalTransitionType) {

  kHYBModalTransitionPresent = 1 << 1,

  kHYBModalTransitionDismiss = 1 << 2

};

@interface HYBModalTransition : NSObject <UIViewControllerAnimatedTransitioning>

/*!

*  @author 黄仪标, 15-12-21 11:12:44

*

*  指定动画类型

*

*  @param type          动画类型

*

*  @return

*/

+ (HYBModalTransition *)transitionWithType:(HYBModalTransitionType)type;

@end

//

//  HYBModalTransition.m

//  PresentDismissTransitionDemo

//

//  Created by huangyibiao on 15/12/21.

//  Copyright © 2015 huangyibiao. All rights reserved.

//

#import “HYBModalTransition.h”

@interface HYBModalTransition ()

@property (nonatomic, assign) HYBModalTransitionType type;

@property (nonatomic, assign) NSTimeInterval duration;

@end

@implementation HYBModalTransition

+ (HYBModalTransition *)transitionWithType:(HYBModalTransitionType)type

{

    HYBModalTransition *transition = [[HYBModalTransition alloc] init];

    transition.type = type;

    transition.duration = 0.35;

    return transition;

}

– (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {

    return 0.35;

}

– (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    switch (self.type) {

        case kHYBModalTransitionPresent: {

            [self present:transitionContext];

            break;

        }

        case kHYBModalTransitionDismiss: {

            [self dismiss:transitionContext];

            break;

        }

        default: {

            break;

        }

    }

}

– (void)present:(id<UIViewControllerContextTransitioning>)transitonContext {

    UIViewController *fromVC = [transitonContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    UIViewController *toVC = [transitonContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = [transitonContext containerView];

    UIView *tempView = [fromVC.view snapshotViewAfterScreenUpdates:NO];

    tempView.frame = fromVC.view.frame;

    fromVC.view.hidden = YES;

    [containerView addSubview:toVC.view];

    [containerView addSubview:tempView];

    toVC.view.frame = CGRectMake(kWidthOfScreen, 0, kWidthOfScreen, kHeightOfScreen);

    [UIView animateWithDuration:self.duration animations:^{

        toVC.view.frame = CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen);

        tempView.frame = CGRectMake(-kWidthOfScreen, 0, kWidthOfScreen, kHeightOfScreen);

        fromVC.view.frame = CGRectMake(-kWidthOfScreen, 0, kWidthOfScreen, kHeightOfScreen);

    } completion:^(BOOL finished) {

        [transitonContext completeTransition:YES];

    }];

}

– (void)dismiss:(id<UIViewControllerContextTransitioning>)transitonContext {

    UIViewController *fromVC = [transitonContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    UIViewController *toVC = [transitonContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = [transitonContext containerView];

    UIView *tempView = containerView.subviews.lastObject;

    [UIView animateWithDuration:self.duration animations:^{

        toVC.view.frame = CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen);

        tempView.frame = CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen);

        fromVC.view.frame = CGRectMake(kWidthOfScreen, 0, kWidthOfScreen, kHeightOfScreen);

    } completion:^(BOOL finished) {

        [transitonContext completeTransition:YES];

        [tempView removeFromSuperview];

        toVC.view.hidden = NO;

    }];

}

@end

//

//  ODDismissNavController.h

//  PresentDismissTransitionDemo

//

//  Created by lixiang on 15/12/24.

//  Copyright © 2015 huangyibiao. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ODDismissNavController : UINavigationController<UIViewControllerTransitioningDelegate>

@end

//

//  ODDismissNavController.m

//  PresentDismissTransitionDemo

//

//  Created by lixiang on 15/12/24.

//  Copyright © 2015 huangyibiao. All rights reserved.

//

#import “ODDismissNavController.h”

#import “HYBModalTransition.h”

@interface ODDismissNavController ()

@end

@implementation ODDismissNavController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.transitioningDelegate = self;

    self.modalPresentationStyle =  UIModalPresentationCustom;

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

– (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

    return [HYBModalTransition transitionWithType:kHYBModalTransitionPresent];

}

– (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {

    return [HYBModalTransition transitionWithType:kHYBModalTransitionDismiss];

}

@end

发表评论