使用Swift编写的iOS上下文转换动画管理器。为每层的动画创建动画任务,并在Union.Delegate上执行任务。
dependencies
。如果您不想使用上述依赖管理器之一,可以将Union手动集成到项目中。
打开终端,使用 cd
命令进入顶级项目目录,然后运行以下命令“if”您的项目不是作为git仓库初始化的
通过运行以下命令将ImageLoader作为git 子模块添加
$ git submodule add https://github.com/TransitionKit/Union.git
我用Union创建了一个示例动画,它来自dribbble.com上的“User-Profile-Interface-Animation”。
查看项目。
动画的单位是Union.Task。有两种创建它的方法
使用层和动画进行初始化
大多数情况是这种模式
let animation = Animation(layer:layer, animation:animation)
使用延迟时间和完成块进行初始化
在不涉及动画的过程中使用
let animation = Animation(delay: 0.3) {
self.view.insertSubview(self.imageView, aboveSubview: self.backgroundView)
}
Union.Animation类
public class Animation {
// public property
public var delay: NSTimeInterval = 0.0 // animation start after delay time
public var completion: () -> () = {} // block called when animation is finished
public init(layer: CALayer, animation: CAPropertyAnimation) {
self.layer = layer
self.animation = animation
}
public init (delay: NSTimeInterval, completion: () -> ()) {
self.delay = delay
self.completion = completion
}
}
通过UINavigationController使用Union.Delegate协议调用动画任务。
Union.Delegate协议
public protocol Delegate {
func animationsBeforeTransitionTo(viewController: UIViewController) -> [Animation]
func animationsDuringTransitionFrom(viewController: UIViewController) -> [Animation]
}
animationsBeforeTransitionTo(viewController: UIViewController) -> [Animation]
在完成这些任务并调用由 UIViewController
显示的此方法后开始过渡。
animationsDuringTransitionFrom(viewController: UIViewController) -> [Animation]
两个 UIViewController
调用的任务在过渡期间开始。所有任务完成后调用:context.completeTransition(true)
extension ViewController: UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController,
animationControllerForOperation operation: UINavigationControllerOperation,
fromViewController fromVC: UIViewController,
toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Union.Navigator.animate()
}
}
extension ViewController: UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Union.Presentor.animate()
}
}
该项目可在 MIT 许可下使用。