自定义模态跳转

Presentation Controller & Device Orientation Animations

提供自定义的模态呈现视图控制器,只要按下面几个步骤:

  1. 创建一个Animator 类(该类定义跳转动画),该类conforms to UIViewControllerAnimatedTransitioning protocol。你要
    • -(void)animateTransition (id)transitionContext
      在此方法里实现跳转动画
    • -(NSTimeInterval)transitionDuration:(id)transitionContext
      在此方法返回跳转动画持续时间
    • -(void)animationEnded:(BOOL)transitionCompleted
      optional, 在此方法里做一些cleanUp
  2. 在ViewController(往往是source view controller, 即调用presentViewController:animated:completion:的controller)中conform UIViewControllerTransitioningDelegate,实现协义方法:
    optional func animationControllerForPresentedController(_ presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    和方法
    optional func animationControllerForDismissedController(_ dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    两个方法都返回第一步实现的Animator类,你可以在同一个Animator类中实现present和dismiss。也可以创建两个Animator类分别实现present和dismiss,并由上述两个函数分别返回。如果你不想使用自定义的模态跳转,返回nil就好了。
  3. present, 在source view controller 中创建presented view controller(就是即将被模态呈现的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
 }

代码有点长,

你可能感兴趣的:(自定义模态跳转)