GTBlurView 是一个 Swift 库,允许以现代和声明式的方式为任何视图添加模糊效果,无论是否具有活力效果。专为基于 UIKit 的 iOS 项目而设计。
要将 GTBlurView 集成到您的项目中,请按照以下步骤操作:
不要忘记在您要使用它的任何地方导入 GTBlurView 模块。
import GTBlurView
// -- 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()
其中 self 和 someView 是 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 值。 其余的都是可选的,只传递实现所需的参数。 另外
actionHandler 闭包可用于在点击模糊视图时触发其他操作。completion 闭包。要将活力效果与模糊效果一起使用,请使用 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 许可下获得许可。