转场动画

presentation使用记录

-(void)tapAction:(UITapGestureRecognizer *)gesture{
    
    SNSSearchViewController *searchResultVC = [[SNSSearchViewController alloc] init];
    searchResultVC.transitioningDelegate  = self;
    searchResultVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:searchResultVC animated:YES completion:nil];
}

#pragma mark - transitionDelegate

//实现transitioningDelegate delegate协议  此处回调对象实现UIViewControllerAnimatedTransitioning协议
- (id)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    return [[SHIModalPresentAnimation alloc] init];
}

animation文件实现协议示例
#import "SHIModalPresentAnimation.h"

@implementation SHIModalPresentAnimation
- (NSTimeInterval)transitionDuration:(id )transitionContext
{
    return 0.35;
}

- (void)animateTransition:(id )transitionContext
{
    // 1. Get controllers from transition context
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 2. Set init frame for toVC
//    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.frame = CGRectOffset(finalFrame,0, 64);
    toVC.view.alpha = 0;
    
    // 3. Add toVC's view to containerView
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    
    // 4. Do animate now
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration/2
                     animations:^{
                        toVC.view.alpha = 1;
                     } completion:^(BOOL finished) {
                         [UIView animateWithDuration:duration/2 animations:^{
                             fromVC.navigationController.navigationBar.hidden = YES;
                             toVC.view.frame = finalFrame;
                         }completion:^(BOOL finished) {
                             // 5. Tell context that we completed.
                             [transitionContext completeTransition:YES];
                         }];
                     }];
}

你可能感兴趣的:(转场动画)