Custom Segues

作者: Girl_iOS | 来源:发表于2015-10-29 22:54 被阅读579次

iOS9中将转场动画和ViewController剥离,可以自定义Segue,从而复用.
Segues默认有几种方式:

  • Show: 从Navigation Controller push一个页面.
  • Show Detail: 向上弹出一个新的页面.
  • Present Modally: 在当前页面覆盖一个新的页面.
  • Popover: iPad弹出个小界面iPhone全屏以Model形式弹出一个界面.
  • Custom:自定义Segue
    关于不同Segue的区别和用法可以参考这里 那里最后这篇
    iOS8中已经不建议使用以前Segue的Push Modal PopoverReplace了.
    我们要来创建一个如下图的转场效果:
ScaleTransition.png

Segue有三个重要的协议:

  • UIViewControllerTransitioningDelegate
  • UIViewControllerAnimatedTransitioning
  • UIViewControllerContextTransitioning
    我们创建一个ScaleSegue文件:
class ScaleSegue: UIStoryboardSegue {
  
  override func perform() {
    destinationViewController.transitioningDelegate = self
    super.perform()
  }
}

定义转场动画:

extension ScaleSegue: UIViewControllerTransitioningDelegate {
  func animationControllerForPresentedController(presented: UIViewController,
     presentingController presenting: UIViewController,
     sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return ScalePresentAnimator()
  }
}
class ScalePresentAnimator: NSObject, UIViewControllerAnimatedTransitioning {
  func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 2.0
  }
  
  func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    var fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
    if let fromNC = fromViewController as? UINavigationController {
      if let controller = fromNC.topViewController {
        fromViewController = controller
      }
    }
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    if let toView = toView {
      transitionContext.containerView()?.addSubview(toView)
    }
    
    var startFrame = CGRect.zero
    if let fromViewController = fromViewController as? ViewScaleable {
      startFrame = fromViewController.scaleView.frame
    } else {
      print("Warning: Controller \(fromViewController) does not conform to ViewScaleable")
    }
    toView?.frame = startFrame
    toView?.layoutIfNeeded()
    
    let duration = transitionDuration(transitionContext)
    let finalFrame = transitionContext.finalFrameForViewController(toViewController)
    
    UIView.animateWithDuration(duration, animations: {
      toView?.frame = finalFrame
      toView?.layoutIfNeeded()
      fromView?.alpha = 0.0
      }, completion: {
        finished in
        fromView?.alpha = 1.0
        transitionContext.completeTransition(true)
    })
  }

最后设置下Segue class:

segueclass.png
Girl学iOS100天 第2天
阅读和写出来果然不太一样,现在写的很费劲,没多少内容,但却花费了3个番茄钟的时间,而且质量不高,但希望自己能坚持.
很喜欢《卖油翁》的一句话"无他'唯手熟尔",共勉 :)

相关文章

  • Custom Segues

    iOS9中将转场动画和ViewController剥离,可以自定义Segue,从而复用.Segues默认有几种方式...

  • iOS自定义转场动画

    在A Beginner’s Guide to Animated Custom Segues in iOS 8通过自...

  • 关于Custom Segues的初探笔记

    开发过程中,我们往往会需要满足各种各样的跳转Segues的需求,光靠自带的那几个默认的转场似乎无法满足成天充满奇思...

  • iOS界面之间的跳转

    此文中我会用 Segues 来完成一个控制器连接到另一个控制器。 Segues 是 Interface Build...

  • 使用Unwind Segues

    翻译自“Using Unwind Segues” 本文档是关于unwind segue的概述。 1 介绍 通过pu...

  • Custom URL Schemes

    Custom URL Schemes Implementing a Custom URL SchemeDefini...

  • jupyter使用sublime快捷键

    添加sublime的快捷键 在~/.jupyter/custom下有custom.css custom.js fo...

  • Segue与Unwind Segues

    使用Segue与Unwind Segues 实现的页面跳转,和回调,以及跳转传值。 UIPopoverPresen...

  • 80课:Segues 进阶

    课程笔记文集地址:Udemy课程:The Complete iOS 9 Developer Course - Bu...

  • Start Developing iOS Apps (Swift

    在本课中,你将使用navigation controller(导航控制器)和segues来创建FoodTracke...

网友评论

本文标题:Custom Segues

本文链接:https://www.haomeiwen.com/subject/qdkfhttx.html