總結(jié)---present/dismiss
modal presentation style
這個(gè)枚舉在iphone上常用的只有三個(gè):fullScreen惜姐, overFullScreen,custom。
下面說明這三個(gè)的區(qū)別:
先點(diǎn)一下窟哺,在轉(zhuǎn)場(chǎng)過程中,系統(tǒng)會(huì)加入一個(gè)過渡控制器:UIPresentationController翰萨。
在自定義轉(zhuǎn)場(chǎng)的過程中,presentingView presentedView 會(huì)被add到過渡控制器的view上糕殉。
然而亩鬼,一個(gè)view只能有一個(gè)父視圖,當(dāng)他被add到過渡控制器上阿蝶,前父視圖自然不再持有它雳锋,這也就導(dǎo)致了會(huì)有返回產(chǎn)生黑屏的情況。
- fullScreen:推出的視圖會(huì)占滿屏幕羡洁。動(dòng)畫結(jié)束后玷过,過渡控制器會(huì)移除add到自身的presentingView。
- overFullScreen:推出的視圖不是占滿屏幕,而且在presentedView的下方可以響應(yīng)操作辛蚊。動(dòng)畫結(jié)束后粤蝎,過渡控制器會(huì)不會(huì)移除add到自身的presentingView。與上方相反袋马。
- custom:完全自定義初澎。需要自定義實(shí)現(xiàn)過渡控制器(協(xié)議中的最后一個(gè)方法),一般繼承自系統(tǒng)的UIPresentationController即可虑凛。同樣不會(huì)移除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
自動(dòng)驅(qū)動(dòng)
前兩個(gè)方法需要返回遵守UIViewControllerAnimatedTransitioning的對(duì)象碑宴,繼承自NSObject即可,下方是實(shí)現(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ù)需求操作兩個(gè)view即可桑谍。
//[UIView animate....] + view的位置只能是通過設(shè)置transForm來決定延柠。
}
其中有個(gè)上下文變量 transitionContext 下方是key 。
UIKIT_EXTERN UITransitionContextViewControllerKey const UITransitionContextFromViewControllerKey
UIKIT_EXTERN UITransitionContextViewControllerKey const UITransitionContextToViewControllerKey
UIKIT_EXTERN UITransitionContextViewKey const UITransitionContextFromViewKey
UIKIT_EXTERN UITransitionContextViewKey const UITransitionContextToViewKey
手勢(shì)驅(qū)動(dòng)器
中間兩個(gè)是需要返回遵守UIViewControllerInteractiveTransitioning的對(duì)象锣披,繼承自UIPercentDrivenInteractiveTransition即可
在需要手勢(shì)驅(qū)動(dòng)的控制器上加上手勢(shì)贞间,在手勢(shì)的selector中根據(jù)手勢(shì)的狀態(tài)調(diào)用下方的方法即可。
第一個(gè)方法中的百分比:根據(jù)手勢(shì)拖拽的距離/屏幕的寬度
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
自定義過渡控制器
繼承自UIPresentationController盈罐,有六個(gè)方法需要重寫
初始化方法榜跌、過渡器進(jìn)入時(shí)、進(jìn)入完成盅粪、消失時(shí)钓葫、消失完畢以及presentedView呈現(xiàn)的frame
過渡器是特殊的控制器呜袁,自身并沒有view色迂。
dimmingView是加在過渡器上的view志膀,再加個(gè)手勢(shì)備用诗芜。如果用戶能夠點(diǎn)擊這個(gè)view资盅,可以給予響應(yīng)回饋攒暇。
- (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
第一個(gè)是手勢(shì)驅(qū)動(dòng)器橘蜜,第二個(gè)是動(dòng)畫驹溃。
實(shí)現(xiàn):代理方法的實(shí)現(xiàn)與上方提到的基本一致含鳞。按需實(shí)現(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;