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

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

2017-06-26  本文已影响300人  Huangbaoqin
  • 创建实现了UINavigationControllerDelegate的对象

1、创建实现了UINavigationControllerDelegate的对象

// CustonNavigationControllerDelegate.m
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPop) {
        return _popAnimation;
    }
    if (operation == UINavigationControllerOperationPush) {
        return _pushAnimation;
    }
    return nil;
}

2、实现动画体

@implementation PushTransitionAnimation

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

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 0;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                     animations:^{
                         [fromViewController.view setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
                         toViewController.view.alpha = 1.0;
                     }
                     completion:^(BOOL finished) {
                         [fromViewController.view setTransform:CGAffineTransformIdentity];
                         [transitionContext completeTransition:YES];
                     }];
}

@end

3、设置NavigationController的代理

// AppDelegate.m
_navDelegate = [[CustonNavigationControllerDelegate alloc] init];
homeNav.delegate = _navDelegate;

Demo地址

上一篇下一篇

猜你喜欢

热点阅读