GTBlurView is a Swift library that allows to add blur effect to any view with or without vibrancy in a modern and declarative fashion. Made for UIKit based projects on iOS.
To integrate GTBlurView
into your projects follow the next steps:
- Copy the repository's URL to GitHub.
- Open your project in Xcode.
- Go to menu File > Swift Packages > Add Package Dependency....
- Paste the URL, select the package when it appears and click Next.
- In the Rules leave the default option selected (Up to Next Major) and click Next.
- Select the GTBlurView package and select the Target to add to; click Finish.
- In Xcode, select your project in the Project navigator and go to General tab.
- Add GTBlurView framework under Frameworks, Libraries, and Embedded Content section.
Don't forget to import GTBlurView
module anywhere you are about to use it:
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:)
The simplest and fastest way to add blur effect to a view is the following:
GTBlurView.addBlur(to: self).show()
// or
GTBlurView.addBlur(to: someView).show()
where self
and someView
are UIView objects.
Blur view can be shown animated as well:
GTBlurView.addBlur(to: someView).showAnimated()
Animation duration and completion handler can be optionally provided. Default animation duration is 0.4 seconds.
GTBlurView.addBlur(to: self).showAnimated(duration: 0.25) {
// do something upon completion...
}
Both show()
and showAnimated(duration:completion:)
methods return a GTBlurView
instance but they are marked with the @discardableResult
attribute. That way assigning it to a variable or property is optional and Xcode will not show any warnings in case you avoid to do so.
Default blur effect style is .regular
. Override it like so:
GTBlurView.addBlur(to: self)
.set(style: .dark)
.show()
It's possible to remove blur view by just tapping on it. Use the removeOnTap(animated:animationDuration:delay:actionHandler:completion:)
method to enable it.
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()
Note that the only required argument is the animated
value. All the rest are optional, and pass only those necessary to your implementation. Also:
actionHandler
closure can be used to trigger additional actions right when the blur view is tapped.completion
closure is called when blur view dismissal is finished.
To use vibrancy effect along with the blur effect use the useVibrancy()
method:
GTBlurView.addBlur(to: self)
.useVibrancy()
.show()
To add subviews to vibrancy's content view call the addVibrancySubview(_:layoutRules:)
method:
let button = UIButton()
// Button configuration.
GTBlurView.addBlur(to: someView)
.useVibrancy()
.addVibrancySubview(button, layoutRules: .auto)
.show()
See the documentation of the VibrancySubviewsLayoutRules
enum
for details about laying out subviews to vibrancy effect view.
One way to remove a blur view is by using the removeOnTap(animated:animationDuration:delay:actionHandler:completion:)
method as shown above. Besides that, there are two ways to manually remove it:
Without animation
GTBlurView.addBlur(to: someView).show()
GTBlurView.remove(from: someView)
With animation
GTBlurView.addBlur(to: someView).show()
GTBlurView.removeAnimated(from: someView)
Animation duration and a completion handler can be optionally provided:
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)
To change blur transparency level you can change the alpha
value of the returned GTBlurView
instance by any of the show()
or showAnimated(duration:completion:)
methods.
Read the documentation of each method using Xcode's Quick Help for additional details.
Current up-to-date version is 1.0.2.
GTBlurView is licensed under the MIT license.