美文网首页
从上面present动画

从上面present动画

作者: th先生 | 来源:发表于2020-11-19 15:27 被阅读0次
    @interface TKTopPresentAnimation : NSObject <UIViewControllerAnimatedTransitioning>
    
    @property (nonatomic, assign) BOOL isPresent;
    
    @end
    
    #import "TKTopPresentAnimation.h"
    
    
    @implementation TKTopPresentAnimation
    
    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
        return 0.375f;
    }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
        
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        
        UIView* toView = nil;
        UIView* fromView = nil;
        UIView* transView = nil;
        
        if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
            fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
            toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        } else {
            fromView = fromViewController.view;
            toView = toViewController.view;
        }
        
        if (_isPresent) {
            transView = toView;
            [[transitionContext containerView] addSubview:toView];
            
        }else {
            transView = fromView;
            [[transitionContext containerView] insertSubview:toView belowSubview:fromView];
        }
        
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        
        transView.frame = CGRectMake(0, _isPresent ? -height : 0, width, height);
        
        WEAKSELF
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            transView.frame = CGRectMake(0, weakSelf.isPresent ? 0 : -height, width, height);
        } completion:^(BOOL finished) {
             [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
        }];
        
    }
    @end
    

    执行动画时,指定代理
    在代理页面<UIViewControllerTransitioningDelegate>

    #pragma  mark -- UIViewControllerTransitioningDelegate
    //指定present动画
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
        
        if ([presented isKindOfClass:NSClassFromString(@"TKVideoCallController")]) {
            TKTopPresentAnimation *animation = [[TKTopPresentAnimation alloc] init];
            animation.isPresent = YES;
            return animation;
        }
        return nil;
        
    }
    //指定dismiss动画
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
        
        if ([dismissed isKindOfClass:NSClassFromString(@"TKVideoCallController")]) {
            TKTopPresentAnimation *animation = [[TKTopPresentAnimation alloc] init];
            animation.isPresent = NO;
            return animation;
        }
        return nil;
    }
    

    或者可以了解一下UIPresentationController,更为强大

    相关文章

      网友评论

          本文标题:从上面present动画

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