您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UINavigationController 反方向滑動轉場動畫實作

編輯:iOS 開發綜合

需求:讓新頁面從右側滑入,而不是由左側推入

系統預設的 push 動畫是新頁面從右邊進來、舊頁面往左退。但某些特殊情境(例如「進入下一層」在視覺上應該往深處走)會希望改成反方向:新頁面從左側飛入。這時候可以替 UINavigationController 寫一個分類來達成。

做法:用分類加入自訂 push

新增一個 UINavigationController+CustomAnimation 分類:

#import "UINavigationController+CustomAnimation.h"

@implementation UINavigationController (CustomAnimation)

- (void)customPushViewController:(UIViewController *)viewController
{
    // 先把新視圖放到畫面左側外面
    viewController.view.frame = (CGRect){
        -viewController.view.frame.size.width, 0,
         viewController.view.frame.size
    };

    // 直接加到目前可見的控制器上,而非真正的 push
    [self.topViewController.view addSubview:viewController.view];

    // 動畫平移回原位
    [UIView animateWithDuration:.35f animations:^{
        viewController.view.frame = (CGRect){0, 0, self.view.bounds.size};
    }];
}

@end
				
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved