present跟push大同小异。需要注意下下面这个@protocol.
UIViewControllerTransitioningDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
专门针对于present.返回自定义的转场协议。
看下custompresentTransition里的内容
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 1;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
//1.获取动画的源控制器和目标控制器
presentViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
presentDViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *container = [transitionContext containerView];
UIView *fromView = fromVC.view;
// UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.frame = CGRectMake(fromView.frame.origin.x, CGRectGetMaxY(fromView.frame)/9.0f, fromView.frame.size.width, fromView.frame.size.height);
toView.alpha = 0;
[container addSubview:toView];
NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:transitionDuration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
toView.alpha = 1;
toView.frame = [transitionContext finalFrameForViewController:toVC];
} completion:^(BOOL finished) {
BOOL wasCancelled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!wasCancelled];
}];
}
demo地址(https://github.com/Butteryflyyer/XH_CustomTransitionl)