目前在 Storyboard 中 从一个 ViewController 到另外一个 ViewController 常用的 Segue 有
- Show
- Show Detail
- Present Modally
- Popover
创建一个 ** 自定义Segue ** 需要需要了解一下术语:
- UIViewControllerTransitioningDelegate : ** 自定义Segue ** 需要实现该协议去管理 presentation 和 dismissal 的 ** 动画对象 **
- UIViewControllerAnimatedTransitioning: ** 动画对象 ** 实行该协议
- UIViewControllerContextTransitioning: 动画的实现细节
本文在完成最后,会实现一个类似于开眼首页点击效果
1 继承UIStoryboard
创建一个空项目, 新建一个名为 SwipeSegue 类,继承于 UIStoryboardSegue. 代码如下:
class SwipeSegue: UIStoryboardSegue {
}
添加一个SwipeSegue 的 extension 实现 UIViewControllerTransitioningDelegate
extension SwipeSegue: UIViewControllerTransitioningDelegate {
}
覆盖 perform() 方法,在方法中设置 destination.transitioningDelegate 为当前的 Segue, 记得调用 super.perform()
class SwipeSegue: UIStoryboardSegue {
override func perform() {
destination.transitioningDelegate = self
super.perform()
}
}
2 创建 Presentation 动画对象 HeadPresentAnimator
创建一个命名 HeadPresentAnimator 的动画对象,实现UIViewControllerAnimatedTransitioning 代理。(最好是把该类放在 segue 类的同一个文件,这样便于管理)
class HeadPresentAnimator: NSObject, UIViewControllerAnimatedTransitioning {
}
打开 UIViewControllerAnimatedTransitioning 可以看到,有两个必须实现的代码方法
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
// 返回动画时间
}public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// 设置动画详情
}
返回动画的时间方法, 设置方法的的返回值为 0.3。当然,如果想仔细看动画效果,可以把返回值设置大一些
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
实现具体动画详情
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: .from)?.targetViewController
let toView = transitionContext.view(forKey: .to)
let toViewController = transitionContext.viewController(forKey: .to)
if toView != nil {
transitionContext.containerView.addSubview(toView!)
}
var startFrame: CGRect = .zero
if let fromViewController = fromViewController as? ViewHeadabel {
startFrame = fromViewController.animatedViewFrame
} else {
print("Waring: from view controller \(String(describing: fromViewController)) does not conform ViewHeadable")
}
toView?.frame = startFrame
toView?.layoutIfNeeded()
let duration = transitionDuration(using: transitionContext)
let finalFrame = transitionContext.finalFrame(for: toViewController!)
UIView.animate(withDuration: duration,
animations: {
toView?.frame = finalFrame
toView?.layoutIfNeeded()
}) { _ in
transitionContext.completeTransition(true)
}
}
** 需要注意的是 **
- 使用方法下面方法获得 toViewController 和 toView, 不要使用 toViewController. view 属性,因为 transition context 处理动画的 presentation 是基于 size class, size class 有 regular-sized 和 compact 的区分, regular-sized 有一个 dimming view。 如果使用 toViewController. view 获取视图 会在一些情况有奇怪现象,具体的详情可以在应用运行的时候更改代码
let toView = toViewController.view
在 Xcode 中点击 ** Debug view hierarchy** 查看查看层级关系
public func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController?
public func view(forKey key: UITransitionContextViewKey) -> UIView?
- 动画完成后调用,它会最终确定视图的层级关系和所以视图的布局
transitionContext.completeTransition(true)
3 Segue 设置 Presentation 动画对象 HeadPresentAnimator
实现 UIViewControllerTransitioningDelegate 代理方法
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return HeadPresentAnimator()
}
4 在 Storyboard 中 替换为刚刚创建的 segue
------------------------------------------------------------------------
到此,一个 presentation 的动画就实现了,dismissal 的创立和 HeadPresentAnimator 非常类似,创建一个 HeadDismissAnimator 的类,具体代码:
class HeadDismissAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: .from)
let fromView = transitionContext.view(forKey: .from)
let toView = transitionContext.view(forKey: .to)
if fromView != nil, toView != nil {
transitionContext.containerView.insertSubview(toView!, belowSubview: fromView!)
}
let duration = transitionDuration(using: transitionContext)
var finalFrame = transitionContext.finalFrame(for: fromViewController!)
if let _ = fromViewController as? ViewSwipeable {
finalFrame.origin.y = UIWindow().bounds.height
}
UIView.animate(withDuration: duration,
animations: {
fromView?.frame = finalFrame
}) { _ in
transitionContext.completeTransition(true)
}
}
}
最后的运行效果:
代码地址