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