@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,更为强大
网友评论