自定義模態(tài)跳轉(zhuǎn)

Presentation Controller & Device Orientation Animations

提供自定義的模態(tài)呈現(xiàn)視圖控制器溺欧,只要按下面幾個(gè)步驟:

  1. 創(chuàng)建一個(gè)Animator 類(該類定義跳轉(zhuǎn)動(dòng)畫(huà))洗贰,該類conforms to UIViewControllerAnimatedTransitioning protocol柿隙。你要
    • -(void)animateTransition (id<UIViewControllerContextTransitioning>)transitionContext
      在此方法里實(shí)現(xiàn)跳轉(zhuǎn)動(dòng)畫(huà)
    • -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
      在此方法返回跳轉(zhuǎn)動(dòng)畫(huà)持續(xù)時(shí)間
    • -(void)animationEnded:(BOOL)transitionCompleted
      optional, 在此方法里做一些cleanUp
  2. 在ViewController(往往是source view controller, 即調(diào)用presentViewController:animated:completion:的controller)中conform UIViewControllerTransitioningDelegate,實(shí)現(xiàn)協(xié)義方法:
    optional func animationControllerForPresentedController(_ presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    和方法
    optional func animationControllerForDismissedController(_ dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?余掖。
    兩個(gè)方法都返回第一步實(shí)現(xiàn)的Animator類寸爆,你可以在同一個(gè)Animator類中實(shí)現(xiàn)present和dismiss。也可以創(chuàng)建兩個(gè)Animator類分別實(shí)現(xiàn)present和dismiss盐欺,并由上述兩個(gè)函數(shù)分別返回赁豆。如果你不想使用自定義的模態(tài)跳轉(zhuǎn),返回nil就好了冗美。
  3. present, 在source view controller 中創(chuàng)建presented view controller(就是即將被模態(tài)呈現(xiàn)的view controller):
    let presentedVC = storyboard!.instantiateViewControllerWithIdentifier(“someIdentifier”) as! PresentedControllerName presentedVC.transitioningDelegate = self presentViewController(herbDetails, animated: true, completion: nil)

 func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

let containerView = transitionContext.containerView()!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

let herbView = presenting ? toView : transitionContext.viewForKey(UITransitionContextFromViewKey)!

let initialFrame = presenting ? originFrame : herbView.frame
let finalFrame = presenting ? herbView.frame : originFrame

let xScaleFactor = presenting ?
  initialFrame.width / finalFrame.width :
  finalFrame.width / initialFrame.width

let yScaleFactor = presenting ?
  initialFrame.height / finalFrame.height :
  finalFrame.height / initialFrame.height

let scaleTransform = CGAffineTransformMakeScale(xScaleFactor, yScaleFactor)

if presenting {
  herbView.transform = scaleTransform
  herbView.center = CGPoint(
    x: CGRectGetMidX(initialFrame),
    y: CGRectGetMidY(initialFrame))
  herbView.clipsToBounds = true
}

containerView.addSubview(toView)
containerView.bringSubviewToFront(herbView)

let herbController = transitionContext.viewControllerForKey(
  presenting ? UITransitionContextToViewControllerKey : UITransitionContextFromViewControllerKey
  ) as! HerbDetailsViewController

if presenting {
  herbController.containerView.alpha = 0.0
}

UIView.animateWithDuration(duration, delay:0.0,
  usingSpringWithDamping: 0.4,
  initialSpringVelocity: 0.0,
  options: [],
  animations: {
    
    herbView.transform = self.presenting ? CGAffineTransformIdentity : scaleTransform
    herbView.center = CGPoint(x: CGRectGetMidX(finalFrame), y: CGRectGetMidY(finalFrame))
    herbController.containerView.alpha = self.presenting ? 1.0 : 0.0
    
  }, completion:{_ in
    
    if !self.presenting {
      self.dismissCompletion?()
    }
    transitionContext.completeTransition(true)
})

  let round = CABasicAnimation(keyPath: "cornerRadius")
  round.fromValue = !presenting ? 0.0 : 20.0/xScaleFactor
  round.toValue = presenting ? 0.0 : 20.0/xScaleFactor
  round.duration = duration / 2
  herbView.layer.addAnimation(round, forKey: nil)
  herbView.layer.cornerRadius = presenting ? 0.0 : 20.0/xScaleFactor
 }

代碼有點(diǎn)長(zhǎng)魔种,

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市粉洼,隨后出現(xiàn)的幾起案子节预,更是在濱河造成了極大的恐慌,老刑警劉巖属韧,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件安拟,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡挫剑,警方通過(guò)查閱死者的電腦和手機(jī)去扣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)柱衔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)樊破,“玉大人愉棱,你說(shuō)我怎么就攤上這事≌芷荩” “怎么了奔滑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)顺少。 經(jīng)常有香客問(wèn)我朋其,道長(zhǎng),這世上最難降的妖魔是什么脆炎? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任梅猿,我火速辦了婚禮,結(jié)果婚禮上秒裕,老公的妹妹穿的比我還像新娘袱蚓。我一直安慰自己,他們只是感情好几蜻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布喇潘。 她就那樣靜靜地躺著,像睡著了一般梭稚。 火紅的嫁衣襯著肌膚如雪颖低。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天弧烤,我揣著相機(jī)與錄音忱屑,去河邊找鬼。 笑死扼褪,一個(gè)胖子當(dāng)著我的面吹牛想幻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播话浇,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼脏毯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了幔崖?” 一聲冷哼從身側(cè)響起食店,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赏寇,沒(méi)想到半個(gè)月后吉嫩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嗅定,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年自娩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渠退。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忙迁,死狀恐怖脐彩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姊扔,我是刑警寧澤惠奸,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站恰梢,受9級(jí)特大地震影響佛南,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嵌言,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一嗅回、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摧茴,春花似錦妈拌、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至丸氛,卻和暖如春培愁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背缓窜。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工定续, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人禾锤。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓私股,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親恩掷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子倡鲸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容