iOS7 自訂動畫跳轉

來源:互聯網
上載者:User

標籤:ios7   動畫   uiviewcontroller   

簡介

在iOS7系統中,如果你使用簡訊或者appStore蘋果原生的工具的時候會發現這麼一個細節。UINavigationViewController中介面的Push不再是像iOS6的舊介面左移,新的介面直接右側滑入,而是新的視圖是從右側滑入並且蓋在了之前視圖控制器之上,還可以通過手勢在介面左側邊界滑動來跳轉到之前的介面。1.1,1.2。


圖1.1                                          圖1.2

不知道你是否已經看出兩幅圖之間的區別。有一個好的訊息告訴大家,在iOS7中,所有和這樣類似的功能都可以通過使用iOS7新的Apis來實現自訂的試圖控制器的跳轉。

一個自訂的跳轉:

當你點擊某一個app中的按鈕的時候,某一個將要呈現的視圖控制器會在當前視圖的底部滑上來。這一次學習,我們將為需要呈現的視圖實現一個自訂的變換。

在iOS中實現自訂跳轉需要執行以下三個步驟:

1.建立一個動畫控制器

第一步是建立一個實現了UIViewControllerAnimatedTransitioning協議的類。這個類中包含的代碼是所要執行的動畫,所以這個類被稱為動畫控制器。

2.在呈現一個新的視圖控制器前,為它設定跳轉的委託。

你需要為你將要呈現的視圖控制器去設定一個它的動畫跳轉委託。通過這個委託回調來擷取呈現新視圖控制器時,自己將要執行的動畫控制器。

3.在回調中返回動畫控制器。

通過回調方法(2中)來返回你建立好的動畫控制器的執行個體(1中)。

之後來實現所有步驟中的第一步:實現一個動畫控制器。

建立一個動畫控制器

    建立一個繼承自NSObject並遵循UIViewControllerAnimatedTransitioning

協議的類。UIViewControllerAnimatedTransitioning協議必須實現兩個用來定義視圖控制器之間自訂動畫的方法。

@interface CustomPresentAnimationController :NSObject <UIViewControllerAnimatedTransitioning>

@end

在.m檔案中添加必須實現的兩個方法:

1.控制整個跳轉動畫所需要執行的時間。例子代碼中執行的時間設定為了兩秒鐘。

- (NSTimeInterval)transitionDuration:
(
id <UIViewControllerContextTransitioning>)transitionContext {

return2.0;

}

2.方法參數transitionContext可以協助你成功的擷取跳轉前後的視圖控制器、內容視圖和少量的其他來龍去脈。你可以遍曆這些屬性,依賴於包含的視圖控制器,使你的跳轉動畫變得與眾不同。

- (void)animateTransition:
(
id <UIViewControllerContextTransitioning>)transitionContext {

// 1. obtain state from the context

UIViewController*toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

CGRectfinalFrame = [transitionContext finalFrameForViewController:toViewController];

// 2. obtain the container view

UIView*containerView = [transitionContext containerView];

// 3. set initial state

CGRectscreenBounds = [[UIScreenmainScreen]bounds]; toViewController.view.frame=

CGRectOffset(finalFrame,0, screenBounds.size.height);// 4. add the view

[containerViewaddSubview:toViewController.view];

// 5. animate

NSTimeIntervalduration =
[self transitionDuration:transitionContext];

[UIViewanimateWithDuration:duration animations:^{

toViewController.view.frame= finalFrame; } completion:^(BOOLfinished) {

// 6. inform the context of completion

[transitionContext completeTransition:YES]; 

}];

}

 點擊關注我,更多精彩內容!!!

群號:336146073

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.