EasyTransitions 是一个库,帮助开发者使用简单函数定义的协议创建自定义交互式过渡,从而避免在 UIKit 中处理多个转换 API。
⚠️ 此库为持续开发中,直到达到 1.0 版本可能会有很多变动⚠️
开发路线图将通过 GitHub 的问题、里程碑和项目进行文档化。欢迎贡献!
🌟 特性
- 自定义转换简单设置
- 支持 Modal 展示、UIPresentationController 和 UINavigationController 转换
- 一行代码支持交互式转换,具有多个拖动手势方向
- 3 个自定义转换示例
- iOS & tvOS
📲 安装
CocoaPods
使用编辑您的 Podfile
并指定依赖关系
pod 'EasyTransitions'
Carthage
使用编辑您的 Cartfile
并指定依赖关系
github "marcosgriselli/EasyTransitions"
👩💻 使用方法
转换
鉴于 UIViewControllerAnimatedTransitioning
对 Modal 展示和 UINavigationController
转换的处理方式不同,我已将功能分为
ModalTransitionConfigurator
NavigationTransitionConfigurator
每个都抓取可用的视图以执行转换。如文档中所述,我避免直接从 UIViewControllers
抓取视图,并通过 UIViewControllerContextTransitioning
的 view(forKey: UITransitionContextViewKey)
访问它们。
模态转换
设计来自Meng To的Design+Code
public protocol ModalTransitionAnimator {
var duration: TimeInterval { get }
var onDismissed: (() -> Void)? { get set }
var onPresented: (() -> Void)? { get set }
func layout(presenting: Bool,
modalView: UIView,
in container: UIView)
func animate(presenting: Bool,
modalView: UIView,
in container: UIView)
}
在进行模态转换时,我们应仅管理被展示的模态视图(modalView)。我们仍然可以在根控制器上注入动画。
ModalTransitionAnimator
是一个非常直接协议。只需在layout
函数中布局需要动画的视图,并在animate
中执行动画。您可以查看AppStoreAnimator来获取一个基本示例。
UINavigationController转换
UINavigationController
采取稍微不同的方法。使用view(forKey: UITransitionContextViewKey)
可以访问from和to视图,所以我们把它们添加到协议函数中。
public protocol NavigationTransitionAnimator {
var duration: TimeInterval { get }
var auxAnimations: (Bool) -> [AuxAnimation] { get set }
func layout(presenting: Bool, fromView: UIView,
toView: UIView, in container: UIView)
func animations(presenting: Bool,
fromView: UIView, toView: UIView,
in container: UIView)
}
我已经将辅助动画作为协议的一部分添加,但预计该领域的更改会很多。
UIPresentationController转换
UIPresentationController
遵循与模态 presentations 相同的方法。您只需要在您的ModalTransitionDelegate
对象上调用func set(presentationController: UIPresentationController?)
即可。
交互
TransitionInteractiveController
处理将一个UIPanGestureRecognizer
连接到当前UIViewController
。这对于任何动作都适用,因为我们应该为交互设置navigationAction: () -> Void
以便交互正常工作。这使我们能够为模态、UINavigationController
或甚至UITabBarController
转换使用相同的TransitionInteractiveController
类。
它的电缆连接需要一个UIViewController
和一个`Pan`对象。EasyTransitions自动处理滑动。它响应速度和完成百分比以完成转换。我计划支持配置此功能。
目前,当我们停止滑动超过50%或者迅速滑动或释放时,转换将完成。如果我们迅速滑动并相反释放,它将自动取消。
平移
平移
枚举是统一UIPanGestureRecognizer
和UIScreenEdgePanGestureRecognizer
的一种简单方式。EasyTransitions内部使用TransitionPanGestureRecognizer
和TransitionEdgePanGestureRecognizer
来支持平移手势
协议,该协议计算每个手势的速度和位移以及其正确的符号。
public enum Pan {
case regular(PanDirection)
case edge(UIRectEdge)
}
示例
在TodayCollectionViewController
上有一个示例,但其他实现中不应有很多变化。以下是一个通用示例:
class MainViewController: UIViewController {
var modalTransitionDelegate = ModalTransitionDelegate()
func showDetailViewController() {
let detailController = DetailViewController()
let animator = MyAnimator() // ModalTransitionAnimator
modalTransitionDelegate.set(animator: animator)
modalTransitionDelegate.wire(viewController: detailController,
with: .regular(.fromTop),
navigationAction: {
detailController.dismiss(animated: true, completion: nil)
})
detailController.transitioningDelegate = modalTransitionDelegate
detailController.modalPresentationStyle = .custom
present(detailController, animated: true, completion: nil)
}
❤️ 贡献
这是一个开源项目,所以请自由地做出贡献。怎么做?
查看所有贡献者
👨💻 作者
Marcos Griselli | @marcosgriselli
🛡 许可证
MIT License
Copyright (c) 2018 Marcos Griselli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.