美文网首页
转场动画

转场动画

作者: 愤怒小鸟飞呀飞 | 来源:发表于2018-07-31 19:23 被阅读0次

    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<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
        
        return [[SHIModalPresentAnimation alloc] init];
    }
    
    animation文件实现协议示例
    #import "SHIModalPresentAnimation.h"
    
    @implementation SHIModalPresentAnimation
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return 0.35;
    }
    
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)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];
                             }];
                         }];
    }
    

    相关文章

      网友评论

          本文标题:转场动画

          本文链接:https://www.haomeiwen.com/subject/thdgmftx.html