GTBlurView

master

Add blur effect to any view in iOS based projects using a simple, modern and declarative way. Available as a Swift package.
gabrieltheodoropoulos/GTBlurView

GTBlurView

Language Platform License Version

Add blur effect to any view in a simple, modern and declarative way.

About

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.

Integrating GTBlurView

To integrate GTBlurView into your projects follow the next steps:

  1. Copy the repository's URL to GitHub.
  2. Open your project in Xcode.
  3. Go to menu File > Swift Packages > Add Package Dependency....
  4. Paste the URL, select the package when it appears and click Next.
  5. In the Rules leave the default option selected (Up to Next Major) and click Next.
  6. Select the GTBlurView package and select the Target to add to; click Finish.
  7. In Xcode, select your project in the Project navigator and go to General tab.
  8. 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

Public 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:)

Usage Example

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.

Blur Effect Style

Default blur effect style is .regular. Override it like so:

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

Remove On Tap

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.

Use Vibrancy

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.

Removing Blur 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...
}

Putting Everything Together

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)

Other

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.

Version

Current up-to-date version is 1.0.2.

License

GTBlurView is licensed under the MIT license.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 11:38:22 GMT-0900 (Hawaii-Aleutian Daylight Time)