Presentation Controller & Device Orientation Animations
提供自定義的模態(tài)呈現(xiàn)視圖控制器溺欧,只要按下面幾個(gè)步驟:
- 創(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
-
- 在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就好了冗美。 - 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)魔种,