一个自定义的模态转场效果,它在一个扩展和收缩的气泡中呈现和消失控制器。
通过 CocoaPods 安装
pod 'BubbleTransition', '~> 3.2.0'
use_frameworks!
通过 Carthage 安装
github "andreamazz/BubbleTransition"
通过 Swift Package Manager 安装
使用 Xcode 集成
File
-> Swift Packages
-> Add Package Dependency...
输入包 URL: https://github.com/andreamazz/BubbleTransition
, 并选择最新的发布版本。
让你的视图控制器遵循 UIViewControllerTransitioningDelegate
协议。设置 transitionMode
, startingPoint
, bubbleColor
和 duration
。
let transition = BubbleTransition()
public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination
controller.transitioningDelegate = self
controller.modalPresentationCapturesStatusBarAppearance = true
controller.modalPresentationStyle = .custom
}
// MARK: UIViewControllerTransitioningDelegate
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.transitionMode = .present
transition.startingPoint = someButton.center
transition.bubbleColor = someButton.backgroundColor!
return transition
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.transitionMode = .dismiss
transition.startingPoint = someButton.center
transition.bubbleColor = someButton.backgroundColor!
return transition
}
你可以在这里找到 Objective-C 的等效实现。
你可以使用交互手势来关闭显示的控制器。要启用此手势,请准备交互式转场
let interactiveTransition = BubbleInteractiveTransition()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? ModalViewController {
controller.transitioningDelegate = self
controller.modalPresentationStyle = .custom
controller.modalPresentationCapturesStatusBarAppearance = true
controller.interactiveTransition = interactiveTransition
interactiveTransition.attach(to: controller)
}
}
并在呈现控制器中实现 interactionControllerForDismissal
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactiveTransition
}
在呈现的控制器中,如果要通过按钮快速关闭,请务必在交互式手势上调用 finish()
。有关更多信息,请查看示例代码。
你可以决定手势的阈值和滑动方向
interactiveTransition.interactionThreshold = 0.5
interactionThreshold.swipeDirection = .up
var startingPoint = CGPointZero
气泡的起始点。
var duration = 0.5
转场持续时间。
var transitionMode: BubbleTranisionMode = .present
转场方向。可以是 .present
, .dismiss
或 .pop
。
var bubbleColor: UIColor = .white
气泡的颜色。请确保它与目标控制器的背景颜色匹配。
查看示例项目以获取完整实现。
Andrea Mazzini。我可以承接自由职业工作,欢迎与我联系。
想要支持 这些免费库 的开发吗? 通过 Paypal 给我买杯咖啡 ☕️ 吧。
感谢 每一位 提交 pull request 的朋友。
Copyright (c) 2018-2020 Andrea Mazzini. All rights reserved.
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.