GTOverlayView 是一个 Swift 库,它允许您快速地向另一个视图添加半透明或彩色的叠加视图。 它提供了一个简单而现代的公共 API,可以通过它进行配置并显示和移除叠加层。
适用于基于 UIKit 的 iOS 项目。
要将 GTOverlayView
集成到您的项目中,请按照以下步骤操作
不要忘记在使用它的任何地方导入 GTOverlayView
模块
import GTOverlayView
// -- Class Methods
// Create a new `GTOverlayView` instance and add it as a subview to the given view.
addOverlay(to:)
// Remove the overlay view from the given view animated.
removeOverlayView(from:duration:)
// -- Instance Methods
// Update the overlay view color.
updateOverlayColor(with:)
// Update the default animation duration when tapping to dismiss the overlay view.
updateRemoveAnimationDuration(with:)
// Set a delay between the time the overlay view is tapped to be removed
// and the time it gets removed.
setDelayWhenRemovingOnTap(_:)
// Show the overlay view animated.
showAnimated(duration:completion:)
// Disable the capability to dismiss the overlay view when tapping on it.
disableRemoveOnTap()
// Provide a closure with custom actions that should be called right when
// the overlay view has disappeared from its super view.
onRemove(handler:)
// Provide a closure with custom actions that should be called when
// the overlay view is tapped.
onTap(handler:)
将叠加视图呈现给另一个视图的最简单方法如下
GTOverlayView.addOverlay(to: self.view).showAnimated()
叠加视图将使用默认持续时间以动画方式呈现。showAnimated()
可以选择性地接受持续时间和完成处理程序参数
GTOverlayView.addOverlay(to: self.view)
.showAnimated(duration: 0.75) {
// Do something when the overlay has been presented...
}
您可以修改叠加视图的各种参数。 例如,以下代码更新了叠加颜色,并在点击叠加层以关闭它时更改了默认动画持续时间
GTOverlayView.addOverlay(to: self.view)
GTOverlayView.addOverlay(to: self.view)
.updateOverlayColor(with: UIColor(red: 0, green: 0.25, blue: 0.75, alpha: 0.75))
.updateRemoveAnimationDuration(with: 0.1)
.showAnimated()
GTOverlayView
允许在点击叠加视图时执行自定义操作。 只需将一个闭包提供给 onTap(handler:)
方法,如下所示
GTOverlayView.addOverlay(to: self.view)
.onTap {
print("Overlay view was tapped!")
// Do something when the overlay view is tapped...
}
.showAnimated()
除了 onTap(handler:)
方法之外,还可以在叠加视图消失时收到通知,因此可以通过将另一个闭包传递给 onRemove(handler:)
方法,在移除时执行任何必要的其他操作
GTOverlayView.addOverlay(to: self.view)
.onRemove {
print("Overlay view has been removed!")
// Do something when the overlay view has been removed...
}
.showAnimated()
可以使用 disableRemoveOnTap()
方法禁用点击时移除叠加层
GTOverlayView.addOverlay(to: self.view)
.disableRemoveOnTap()
.showAnimated()
要手动移除叠加视图,请调用 removeOverlayView(from:duration:)
类方法
GTOverlayView.removeOverlayView(from: self.view)
// or, overriding the default duration:
GTOverlayView.removeOverlayView(from: self.view, duration: 0.4)
有关每个可用方法的更多详细信息,请参阅 Xcode 的快速帮助。
默认叠加颜色是半透明的灰色 (UIColor(white: 0.5, alpha: 0.5)
)。 使用 updateOverlayColor(with:)
方法更改它。
除了 removeOverlayView(from:duration:)
类方法(这是一个 void
方法)之外,上述所有方法都返回一个 GTOverlayView
实例。
将初始化的 GTOverlayView
实例分配给变量或属性是可选的,因为所有返回它的方法都已标记为 discardableResult
属性。
建议最后调用 showAnimated(duration:completion:)
方法。
当前版本为 1.0.0。
GTOverlayView 在 MIT 许可下获得许可。