ios 8 下可结合UIPresentation 和UIViewControllerTransitioningDelegate
ios 7 下无UIPresentation(与ios 8下类似).
以下是在ios8下实现的:
1. 设置presentedvc 的modalPresentationStyle属性为UIModalPresentationCustom,并设置transitioningDelegate;
- UIModalPresentationCustom告诉系统presentedvc的transition将是自定义的;
- transitioningDelegate告诉系统处理自定义transition的代理是谁
- transitioningDelegate需实现UIViewControllerTransitioningDelegate协议
2. 实现transitioningDelegate的UIViewControllerTransitioningDelegate协议
a. 提供UIPresentationController(用来管理转场过程,下文详述)
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
b. 提供转场动画(animator(需要遵守UIViewControllerAnimatedTransitioning协议))(下文讲详述)
// presentation 动画 - (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source // dismiss 动画 - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
3. 用UIPresentationController管理转场过程
1)根据需要重写以下方法,控制转场过程
- (void)presentationTransitionWillBegin; // 可在此方法中向试图层级中(containerview)增加custom view,并为custom view 添加动画(如增加dimming view) // 添加动画可用以下方法实现 //- (BOOL)animateAlongsideTransition:(void (^)(id<UIViewControllerTransitionCoordinatorContext>context))animation completion:(void (^)(id<UIViewControllerTransitionCoordinatorContext>context))completion; //或者 //- (BOOL)animateAlongsideTransitionInView:(UIView *)view animation:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))animation completion:(void (^)(id<UIViewControllerTransitionCoordinatorContext>context))completion; - (void)presentationTransitionDidEnd:(BOOL)completed; // 如果completed 为no,可以清理视图层级 - (void)dismissalTransitionWillBegin; // 可控制custom view的动画 - (void)dismissalTransitionDidEnd:(BOOL)completed; // 可清理视图层级
2)控制containerview中view的大小
-(CGRect)frameOfPresentedViewInContainerView //设定presentation 完成时presneted view 的大小 -(CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize // 此为uicontentcontainer中的协议方法(每个viewcotroller 和UIPresentationController遵守该协议),返回ChildContentContainer的大小
3)layout
-(void)containerViewWillLayoutSubviews //可用来控制旋转时,containerView中各view 的大小
4. 提供转场动画,主要实现以下两个方法
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext; // 告知动画持续时间 - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext; // 告知如何动画,利用transitionContext提供的信息,在动画中改变transition涉及的view的geometry。 // 可以讲[transitionContext containerView] 看作动画板,可在其中添加,移除view,并改变geometry // 如果是present,containerView不包含toview,需要手动添加