首页投稿(暂停使用,暂停投稿)iOS Developer

iOS开发-自定义Modal转场效果

2017-06-26  本文已影响449人  Huangbaoqin

A(AViewController)通过Present方式转场到B(BViewController)

// AViewController.m
- (IBAction)handleNextButtonPressed:(id)sender {
    BViewController *bVC = [BViewController new];
    bVC.transitioningDelegate = self;
    bVC.modalPresentationStyle = UIModalPresentationCustom;// present之后不会移除AViewController.view,因此dismiss动画对象中不用添加toView了
    [self presentViewController:bVC animated:YES completion:^{
        
    }];
}
// AViewController.m
// 实现UIViewControllerTransitioningDelegate协议中的方法
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return _presentAnimation; //_presentAnimatino为实现了UIViewControllerAnimatedTransitioning协议的动画对象
}
// PresentAnimation.m
@implementation PresentAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.6;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    
    [containerView addSubview:toVC.view];
    
    CGRect targetFrame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.frame = CGRectOffset(targetFrame, CGRectGetWidth(targetFrame), -CGRectGetHeight(targetFrame));
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toVC.view.frame = targetFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end

B(BViewController)通过dismiss方式转场到A(AViewController)

// BViewController.m
- (IBAction)handeBackButtonPressed:(id)sender {
    self.transitioningDelegate = self;// 抢回Delegate
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
// BViewController.m
// 实现UIViewControllerTransitioningDelegate协议中的方法
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return _dismissAnimation;
}
// DismissAnimation.m
@implementation DismissAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.6;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    CGRect targetFrame = [transitionContext initialFrameForViewController:fromVC];
    targetFrame = CGRectOffset(targetFrame, CGRectGetWidth(targetFrame), - CGRectGetHeight(targetFrame));
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromVC.view.frame = targetFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end

Demo地址

上一篇 下一篇

猜你喜欢

热点阅读