国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS 控制器自定義動畫跳轉方法(模態跳轉)

2019-10-21 18:42:25
字體:
來源:轉載
供稿:網友

參考資料:

Apple 開發文檔 Customizing the Transition Animations

WWDC 2013 Custom Transitions Using View Controllers

圖例:

跳轉動畫有很多,全部可以自定義

iOS,控制器,自定義,動畫,跳轉

創建自定義跳轉必須遵循的三個步驟:

1、創建一個類,并實現了 UIViewControllerAnimatedTransitioning 協議

2、創建一個類作為 UIViewControllerTransitioningDelegate 過渡代理

3、在模態跳轉前修改控制器的 transitioningDelegate 代理為自定義的代理(步驟2的代理類)

核心代碼示例

一、創建一個類,并實現了 UIViewControllerAnimatedTransitioning 協議

這個協議主要控制控制器視圖的顯示的,通過 transitionContext 可以獲取到每個視圖和控制器,并進行動畫的設置

class AnimatedTransitioning: NSObject { var isPresenting: Bool = false}extension AnimatedTransitioning: UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.5 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let fromView = transitionContext.view(forKey: .from)! let toView = transitionContext.view(forKey: .to)! let containerView = transitionContext.containerView if isPresenting {  toView.transform = CGAffineTransform(scaleX: 0, y: 0)  containerView.addSubview(toView) } else {  containerView.insertSubview(toView, belowSubview: fromView) } UIView.animate(withDuration: 0.5, animations: {  if self.isPresenting {  toView.transform = CGAffineTransform.identity  } else {  fromView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)  } }) { (finished) in  transitionContext.completeTransition(finished) } }}

二、創建一個類作為 UIViewControllerTransitioningDelegate 過渡代理

這里設置 presented 和 dismissed 時各自的動畫轉換類,可以設置為不同的類

class CustomTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { let at = AnimatedTransitioning() at.isPresenting = true return at } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { let at = AnimatedTransitioning() at.isPresenting = false return at }}

三、在模態跳轉前修改控制器的 transitioningDelegate 代理為自定義的代理

注意:代理不能為局部變量

class ViewController: UIViewController { // 必須保存為實例變量 var ctDelegate = CustomTransitioningDelegate() override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let vc = TempViewController() vc.transitioningDelegate = ctDelegate self.present(vc, animated: true, completion: nil) }}

以上這篇iOS 控制器自定義動畫跳轉方法(模態跳轉)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富裕县| 建始县| 新竹县| 西林县| 天祝| 海兴县| 清徐县| 甘南县| 定边县| 长垣县| 肥西县| 白朗县| 资溪县| 通河县| 贡嘎县| 武安市| 丹阳市| 河曲县| 土默特右旗| 丰镇市| 徐闻县| 明星| 万全县| 阿拉善右旗| 南溪县| 丹凤县| 济宁市| 紫金县| 洛阳市| 岳普湖县| 新竹县| 吉林省| 平江县| 乐都县| 鹤壁市| 晋中市| 阿拉善左旗| 那坡县| 体育| 舞钢市| 黑山县|