GuillotineMenu.swift
受以下 Dribbble 项目的启发:https://dribbble.com/shots/2018249-Side-Topbar-Animation
另外,您可以在我们的 博客 中查看实现细节。
需求
- iOS 8.0+
- Xcode 10
- Swift 5.0 (v 4.1+)
- Swift 4.2 (v 4.0)
安装
CocoaPods
pod 'GuillotineMenu'
手动安装
您可以在示例文件夹中查看项目的完整操作示例。
- 您必须将 "GuillotineMenuTransitionAnimation.swift" 添加到您的项目中,仅此而已。
用法
- 现在,取决于您,是否让您的菜单从屏幕的左上角还是从您的导航栏处下降,因为如果您想要像示例中的动画,就必须让您的菜单视图控制器符合 "GuillotineMenu" 协议。当您符合此协议时,您必须制作一个菜单按钮和标题,您不需要制作它们的框架,因为动画器会自动做。
- 在将显示菜单的视图控制器中,您必须为 "GuillotineMenuTransitionAnimator" 制作一个属性。这是显示或关闭菜单时进行正确动画所必需的。
- 当您显示菜单时,您必须确保模型表示样式的设置为自定义,并将菜单的转换代表设置为显示菜单的视图控制器
let menuViewController = storyboard!.instantiateViewController(withIdentifier: "MenuViewController")
menuViewController.modalPresentationStyle = .custom
menuViewController.transitioningDelegate = self
- 在您的表示视图控制器中实现 UIViewControllerTransitionDelegate 方法
extension ViewController: UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
presentationAnimator.mode = .presentation
return presentationAnimator
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
presentationAnimator.mode = .dismissal
return presentationAnimator
}
- 最后,您可以分配从菜单将会降落的偏移视图和相应的按钮,并显示您的菜单
presentationAnimator.supportView = navigationController!.navigationBar
presentationAnimator.presentButton = sender
present(menuViewController, animated: true, completion: nil)
定制
当然,您可以给菜单分配不同的 "supportView" 或 "presentButton",我们认为像示例项目中的行为是最好的。
要指定动画效果的距离,更改 "duration" 属性的值。
您还有动画器的优秀的委托方法。
public protocol GuillotineAnimationDelegate: class {
func animatorDidFinishPresentation(_ animator: GuillotineTransitionAnimation)
func animatorDidFinishDismissal(_ animator: GuillotineTransitionAnimation)
func animatorWillStartPresentation(_ animator: GuillotineTransitionAnimation)
func animatorWillStartDismissal(_ animator: GuillotineTransitionAnimation)
}
当菜单动画时,您可以做任何您想做的事情。
让我们知道!
如果您在项目中使用我们的组件,请将链接发送给我们。请发送电子邮件至 [email protected] ,并告知我们您是否有关于动画的任何问题或建议。
附言:我们将发布更多的绝妙代码和关于如何使 iOS(Android)的 UI 优于普通的教程。敬请期待!
许可协议
The MIT License (MIT)
Copyright © 2017 Yalantis
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.