iOS 轉場動畫 present


在項目中沒有創建 UINavgationController,無法使用默認的 push 方法 進行頁面的跳轉時。

使用另一種頁面跳轉方法 :

  - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);

常用的方法:

 // 結合控件的點擊事件,例:常用的UIButton UIButton * button = [UIButton alloc] init]; [button addTarget:self action:@selector(buttonClick) forControlEvents:(UIControlEventTouchUpInside)]; // 默認跳轉方式,從底部推出 - (void)buttonClick { UIViewController * viewController = [UIViewController alloc] init]; [self presentViewController: viewController animated:YES completion:nil]; }

根據需求設置跳轉時的樣式:

- (void)buttonClick { CATransition * animation = [CATransition animation]; animation.duration = 0.5; // 時間 /** type:動畫類型 * pageCurl 向上翻一頁 * pageUnCurl 向下翻一頁 * rippleEffect 水滴 * suckEffect 收縮 * cube 方塊 * oglFlip 上下翻轉 */ animation.type = @"pageCurl"; /** type:頁面轉換類型 * kCATransitionFade 淡出 * kCATransitionMoveIn 覆蓋 * kCATransitionReveal 底部顯示 * kCATransitionPush 推出 */ animation.type = kCATransitionPush; //PS:type 更多效果請 搜索: CATransition /** subtype:出現的方向 * kCATransitionFromRight 右 * kCATransitionFromLeft 左 * kCATransitionFromTop 上 * kCATransitionFromBottom 下 */ animation.subtype = kCATransitionFromRight; [self.view.window.layer addAnimation:animation forKey:nil]; // 添加動作 [self presentViewController: viewController animated:YES completion:nil]; // 跳轉 } // PS:設置 type 屬性時,兩種寫法

在 UIViewController 中才可以調用 presentViewController 方法。
但如果是監聽 [自定義View] 中的一個 [UIButton事件] 進行跳轉:

 // 自定義 View.h 文件中 @property (nonatomic, strong) UIViewController * viewController; --------------------- // 自定義 View.m 文件中 // 自定義View視圖中的 UIButton self.button = [UIButton alloc] init]; [self.button addTarget:self action:@selector(selfButtonClick:) forControlEvents:(UIControlEventTouchUpInside)]; // self.button 點擊方法 - (void)selfButtonClick:(UIViewController *)otherVC { otherVC = self.viewController; // do soming [self presentViewController:otherVC animated:YES completion:nil]; // 跳轉 } ------------------- // 綁定 自定義View 的 Controller 中 self.自定義View.viewController = self;



文/FFynn(簡書作者)
原文鏈接:http://www.jianshu.com/p/26a57835ef3c
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM