GTOverlayView

Language Platform License Version

在 iOS 应用程序中的任何视图上方显示可定制的叠加视图。

关于

GTOverlayView 是一个 Swift 库,它允许您快速地向另一个视图添加半透明或彩色的叠加视图。 它提供了一个简单而现代的公共 API,可以通过它进行配置并显示和移除叠加层。

适用于基于 UIKit 的 iOS 项目。

集成 GTOverlayView

要将 GTOverlayView 集成到您的项目中,请按照以下步骤操作

  1. 复制存储库的 GitHub URL。
  2. 在 Xcode 中打开您的项目。
  3. 转到菜单 File > Swift Packages > Add Package Dependency...
  4. 粘贴 URL,选择出现的包,然后单击“Next”。
  5. 规则中,保留选定的默认选项 (Up to Next Major),然后单击“Next”。
  6. 选择 GTOverlayView 包,然后选择要添加到的目标;单击“Finish”。
  7. 在 Xcode 中,在项目导航器中选择您的项目,然后转到General选项卡。
  8. Frameworks, Libraries, and Embedded Content部分下添加 GTOverlayView 框架。

不要忘记在使用它的任何地方导入 GTOverlayView 模块

import GTOverlayView

公共 API

// -- 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()

GTOverlayView Sample

叠加视图将使用默认持续时间以动画方式呈现。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)

备注

版本

当前版本为 1.0.0。

许可

GTOverlayView 在 MIT 许可下获得许可。