您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 開發:UINavigationController 反方向滑動 push

編輯:iOS 相關資訊

有時我們希望 push 新頁面時採用與系統預設相反的滑動方向(例如從右側滑入)。可以透過為 UINavigationController 建立類別(Category)來實現這個自訂動畫。

建立類別並實作自訂 push

#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};
    [self.topViewController.view addSubview:viewController.view];
    [UIView animateWithDuration:.35f animations:^{
        viewController.view.frame = (CGRect){0, 0, self.view.bounds.size};
    }];
}
@end

這種做法是將新的 View 加到 self.navigationController.topViewController 上,並搭配滑動動畫。呼叫時,只要使用 customPushViewController: 取代原本的 pushViewController: 即可。

作為 pop 動畫使用

若想要在返回時也套用類似動畫,可以使用 begin/commit 動畫方式:

- (void)backToHome
{
    [UIView beginAnimations:@"popView" context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:.5f];
    [UIView setAnimationDidStopSelector:@selector(popAnimationDidStop)];
    self.view.frame = CGRectMake(-CGRectGetWidth(self.view.frame), 0,
                                 CGRectGetWidth(self.view.frame),
                                 CGRectGetHeight(self.view.frame));
    [UIView commitAnimations];
}

透過以上方式,即可為 UINavigationController 的頁面推入與返回加上自訂方向與滑動動畫,讓導航過渡更具個人風格。

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