總結(jié)---present/dismiss
modal presentation style
這個枚舉在iphone上常用的只有三個:fullScreen, overFullScreen,custom。
下面說明這三個的區(qū)別:
先點一下,在轉(zhuǎn)場過程中,系統(tǒng)會加入一個過渡控制器:UIPresentationController。
在自定義轉(zhuǎn)場的過程中,presentingView presentedView 會被add到過渡控制器的view上。
然而,一個view只能有一個父視圖,當他被add到過渡控制器上,前父視圖自然不再持有它,這也就導致了會有返回產(chǎn)生黑屏的情況。
- fullScreen:推出的視圖會占滿屏幕。動畫結(jié)束后,過渡控制器會移除add到自身的presentingView。
- overFullScreen:推出的視圖不是占滿屏幕,而且在presentedView的下方可以響應操作。動畫結(jié)束后,過渡控制器會不會移除add到自身的presentingView。與上方相反。
- custom:完全自定義。需要自定義實現(xiàn)過渡控制器(協(xié)議中的最后一個方法),一般繼承自系統(tǒng)的UIPresentationController即可。同樣不會移除presentingView
UIViewControllerTransitioningDelegate
vc.transitioningDelegate = self;
vc.modalPresentationStyle = @"枚舉";
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source
自動驅(qū)動
前兩個方法需要返回遵守UIViewControllerAnimatedTransitioning的對象,繼承自NSObject即可,下方是實現(xiàn)
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.5;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
UIView *view = [transitionContext containerView];
__block UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
__block UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
[view addSubview:fromView];
[view addSubview:toView];
//根據(jù)需求操作兩個view即可。
//[UIView animate....] + view的位置只能是通過設置transForm來決定。
}
其中有個上下文變量 transitionContext 下方是key 。
UIKIT_EXTERN UITransitionContextViewControllerKey const UITransitionContextFromViewControllerKey
UIKIT_EXTERN UITransitionContextViewControllerKey const UITransitionContextToViewControllerKey
UIKIT_EXTERN UITransitionContextViewKey const UITransitionContextFromViewKey
UIKIT_EXTERN UITransitionContextViewKey const UITransitionContextToViewKey
手勢驅(qū)動器
中間兩個是需要返回遵守UIViewControllerInteractiveTransitioning的對象,繼承自UIPercentDrivenInteractiveTransition即可
在需要手勢驅(qū)動的控制器上加上手勢,在手勢的selector中根據(jù)手勢的狀態(tài)調(diào)用下方的方法即可。
第一個方法中的百分比:根據(jù)手勢拖拽的距離/屏幕的寬度
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
自定義過渡控制器
繼承自UIPresentationController,有六個方法需要重寫
初始化方法、過渡器進入時、進入完成、消失時、消失完畢以及presentedView呈現(xiàn)的frame
過渡器是特殊的控制器,自身并沒有view。
dimmingView是加在過渡器上的view,再加個手勢備用。如果用戶能夠點擊這個view,可以給予響應回饋。
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
if ((self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController])) {
_dimmingView = [[UIView alloc] init];
_dimmingView.backgroundColor = [UIColor clearColor];
_dimmingView.alpha = 1;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]bk_initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
if (self.touchDimmingView) {
self.touchDimmingView();
}
}];
[_dimmingView addGestureRecognizer:tap];
}
return self;
}
- (void)presentationTransitionWillBegin
{
_dimmingView.frame = self.containerView.bounds;
[self.containerView addSubview:_dimmingView];
[self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{
if (!completed) {
[_dimmingView removeFromSuperview];
}
}
- (void)dismissalTransitionWillBegin
{
[UIView animateWithDuration:0.3 animations:^{
_dimmingView.alpha = 0;
}];
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
if (completed) {
[_dimmingView removeFromSuperview];
}
}
-(CGRect)frameOfPresentedViewInContainerView
{
return self.presentContnetSize;
}
總結(jié)--push/pop
第一個是手勢驅(qū)動器,第二個是動畫。
實現(xiàn):代理方法的實現(xiàn)與上方提到的基本一致。按需實現(xiàn)即可。
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController;
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC;