简介
ios中present controller之后,上一层的controller的内容是不可见的,但是有的弹框需要能看到上一层的内容,这时候我们就要重写present的动画效果,然后手动的改变present动作。
UIViewControllerTransitioningDelegate
这个代理主要是改变一些系统的自定义动画,其中有一个- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
方法,这个方法会返回一个动画效果,也就是present这个动作的动画效果,如果不重写,会自动用系统的。
重写方法的实现:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
//这里面AnimatedTransitioning是自己自定义实现的动画效果
AnimatedTransitioning *controller = [[AnimatedTransitioning alloc]init];
controller.isPresenting = YES;
return controller;
}
再看看AnimatedTransitioning的实现:
@protocol AnimatedTransitioningProtocol <NSObject>
- (void)startAnimation;
@end
@interface AnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) BOOL isPresenting;
@end
#import "AnimatedTransitioning.h"
@implementation AnimatedTransitioning
//===================================================================
// - UIViewControllerAnimatedTransitioning
//===================================================================
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 0.25f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
//获取当前发出present动作的controller的View
UIView *inView = [transitionContext containerView];
//获取到将要present的controller
UIViewController<AnimatedTransitioningProtocol> *toVC = (UIViewController<AnimatedTransitioningProtocol> *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//获取到发出present动作的controller
UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
//讲要去的VC的view添加到触发present动作的VC上面,这样上一层的VC的内容就不会消失了
[inView addSubview:toVC.view];
//设置要present VC的view的frame
[toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
//这时候调用自定义的一个代理,手动的写弹框vc上面内容的动画
[toVC startAnimation];
//注意这个一定要设置,如果没有使用,系统会不知道当前的transition是否已经结束,这样造成的后果:使app进入某种未知状态,比如presentingViewController能看到新view但是无法和用户交互。
[transitionContext completeTransition:YES];
}
@end
有待改进,如有错误,还请大神指正。