iOS, blur and UIVisualEffectView using AFBlurView

From iOS7 everything now is about blur. In iOS7 it wasn’t easy to blur a view in “real time”, there are a lot of libraries on GitHub that can  (almost) do that.
On iOS8 Apple came to rescue enabling the UIVisualEffectView.

Let’s see how to implement it.

Create a UIVisualEffectView is easy, you just need to create a UIVisualEffect and pass it to the UIVisualEffectView initilizer. Let’s say that we want to build a blurred view:

UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView * visualEffectBlurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.view addSubview:visualEffectBlurView];

// Install constraints

Now if we want to add a view to the visualEffectView we need to add it as a subview of its -contentView, moreover if we want to add the vibrancy effect to make content more clear we need to create another effect view with a vibrancy effect and add it as a subview of the blur content view.

UIVisualEffect *vibrancyEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView * visualEffectVibrancyView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
[visualEffectBlurView.contentView addSubview:visualEffectVibrancyView];

// Install constraints

All the subview that we are going to add as a subview of the vibrancy  -contentView will have both vibrancy and blur enabled.
As you can see it seems a little bit confusing, that’s why I built AFBlurView.
AFBlurView is a subclass of UIView with simple facilities that enable/disable visual effects.

You can either use it in xib documents (I must say that Visual Effect Views are also pickable from the object list) or programmatically, furthermore you can install one inside an already existing hierarchy without modify your code.

The source code is available on github under MIT license:

Sorry, the comment form is closed at this time.

Related Posts