GTBlurView

Language Platform License Version

以简单、现代和声明式的方式为任何视图添加模糊效果。

关于

GTBlurView 是一个 Swift 库,允许以现代和声明式的方式为任何视图添加模糊效果,无论是否具有活力效果。专为基于 UIKit 的 iOS 项目而设计。

集成 GTBlurView

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

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

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

import GTBlurView

公共 API

// -- Class Methods

// Create a `GTBlurView` view with a blur effect visual effect view
// and add it to the given parent view.
addBlur(to:)

// Remove the `GTBlurView` instance and all of its subviews from the given view.
remove(from:)

// Remove `GTBlurView` instance and all of its subviews from the given view animated.
removeAnimated(from:duration:completion:)


// -- Instance Methods

// Change the default blur style.
set(style:)

// Add vibrancy to blur effect.
useVibrancy()

// Add a subview to vibrancy effect view.
addVibrancySubview(_:layoutRules:)

// Remove `GTBlurView` instance and its contents on tap.
removeOnTap(animated:animationDuration:actionHandler:completion:)
 
// Show the `GTBlurView` instance containing the blur effect without animation.
show()

// Show the `GTBlurView` instance containing the blur effect view animated.
showAnimated(duration:completion:)

使用示例

向视图添加模糊效果的最简单和最快的方法如下:

GTBlurView.addBlur(to: self).show()

// or

GTBlurView.addBlur(to: someView).show()

其中 selfsomeView 是 UIView 对象。

模糊视图也可以动画显示:

GTBlurView.addBlur(to: someView).showAnimated()

可以有选择地提供动画持续时间和完成处理程序。默认动画持续时间为 0.4 秒。

GTBlurView.addBlur(to: self).showAnimated(duration: 0.25) {
    // do something upon completion...
}

show()showAnimated(duration:completion:) 方法都返回一个 GTBlurView 实例,但它们都标有 @discardableResult 属性。 这样,将其分配给变量或属性是可选的,并且如果您避免这样做,Xcode 将不会显示任何警告。

模糊效果样式

默认的模糊效果样式为 .regular。像这样覆盖它:

GTBlurView.addBlur(to: self)
    .set(style: .dark)
    .show()

点击移除

可以通过点击来移除模糊视图。 使用 removeOnTap(animated:animationDuration:delay:actionHandler:completion:) 方法来启用它。

GTBlurView.addBlur(to: self)
    .removeOnTap(animated: true, animationDuration: 0.25, actionHandler: {
        // Do something when tapping on GTBlurView instance...
    }, completion: {
        // Do something when GTBlurView instance has been removed...
    })
    .show()

请注意,唯一必需的参数是 animated 值。 其余的都是可选的,只传递实现所需的参数。 另外

使用活力效果

要将活力效果与模糊效果一起使用,请使用 useVibrancy() 方法。

GTBlurView.addBlur(to: self)
    .useVibrancy()
    .show()

要将子视图添加到活力效果的内容视图,请调用 addVibrancySubview(_:layoutRules:) 方法。

let button = UIButton()
// Button configuration.

GTBlurView.addBlur(to: someView)
   .useVibrancy()
   .addVibrancySubview(button, layoutRules: .auto)
   .show()

有关将子视图布局到活力效果视图的详细信息,请参阅 VibrancySubviewsLayoutRules enum 的文档。

移除模糊视图

删除模糊视图的一种方法是使用上面显示的 removeOnTap(animated:animationDuration:delay:actionHandler:completion:) 方法。 除此之外,还有两种手动移除它的方法:

不带动画

GTBlurView.addBlur(to: someView).show()
GTBlurView.remove(from: someView)

带动画

GTBlurView.addBlur(to: someView).show()
GTBlurView.removeAnimated(from: someView)

可以有选择地提供动画持续时间和完成处理程序。

GTBlurView.addBlur(to: someView).show()

GTBlurView.removeAnimated(from: someView, duration: 0.25) {
    // do something once blur view has been removed...
}

将所有内容放在一起

GTBlurView.addBlur(to: self)
    .set(style: .regular)
    .useVibrancy()
    .addVibrancySubview(someView, layoutRules: .none)
    .removeOnTap(animated: true, animationDuration: 0.25, actionHandler: {
        // Do something when tapping on GTBlurView instance...
    }, completion: {
        // Do something when GTBlurView instance has been removed...
    })
    .showAnimated(duration: 0.25)

其他

要更改模糊透明度级别,您可以更改任何 show()showAnimated(duration:completion:) 方法返回的 GTBlurView 实例的 alpha 值。

使用 Xcode 的快速帮助阅读每种方法的文档,以获取更多详细信息。

版本

当前最新版本是 1.0.2。

许可证

GTBlurView 在 MIT 许可下获得许可。