轉(zhuǎn)場動畫

總結(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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,247評論 6 543
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,520評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,362評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,805評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,541評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,896評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,887評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,062評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,608評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,356評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,555評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,077評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,769評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,175評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,489評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,289評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,516評論 2 379