Use UIVisualEffectView to add special effects to views

Use UIVisualEffectView to add special effects to views

Use UIVisualEffectView to add special effects to views

After iOS 8, Apple opened up a number of interfaces for creating special effects, including an interface for creating frosted glass (blur).

Usually, if you want to create a special effect (such as blur effect), you can create a UIVisualEffectView view object, which provides a simple way to achieve complex visual effects. This object can be regarded as a container for the effect. The actual effect will affect the content under the view object, or the content added to the contentView of the view object.

Let's take an example to see how to use UIVisualEffectView:

  1. let bgView: UIImageView = UIImageView(image: UIImage(named: "visual" ))
  2. bgView.frame = self.view.bounds
  3. self.view.addSubview(bgView)
  4.  
  5. let blurEffect: UIBlurEffect = UIBlurEffect(style: .Light)
  6. let blurView: UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
  7. blurView.frame = CGRectMake( 50.0 , 50.0 , self.view.frame.width - 100.0 , 200.0 )
  8. self.view.addSubview(blurView)

This code adds a UIImageView as the background image to the current view controller.

We can see that UIVisualEffectView is very simple. It should be noted that subviews should not be added directly to the UIVisualEffectView view, but should be added to the contentView of the UIVisualEffectView object.

In addition, try to avoid setting the alpha value of the UIVisualEffectView object to a value less than 1.0, because creating a semi-transparent view will cause the system to perform blending operations on the UIVisualEffectView object and all related subviews during off-screen rendering. This not only consumes CPU/GPU, but may also cause many effects to display incorrectly or not display at all.

We saw above that the method to initialize a UIVisualEffectView object is UIVisualEffectView(effect: blurEffect), which is defined as follows:

  1. init(effect effect: UIVisualEffect)

The parameter of this method is a UIVisualEffect object. Looking at the official documentation, we can see that in UIKit, several objects are defined specifically for creating visual effects, namely UIVisualEffect, UIBlurEffect, and UIVibrancyEffect. Their inheritance hierarchy is as follows:

  1. NSObject
  2. |--UIVisualEffect
  3. |--UIBlurEffect
  4. |--UIVibrancyEffect

UIVisualEffect is a base class for creating visual effects that inherits from NSObject. However, this class does not provide any new properties and methods other than those inherited from NSObject. Its main purpose is to initialize UIVisualEffectView, in which you can pass in UIBlurEffect or UIVibrancyEffect objects.

A UIBlurEffect object is used to apply a blur effect to the content below the UIVisualEffectView view, as shown in the example above. However, the effect of this object does not affect the content in the contentView of the UIVisualEffectView object.

UIBlurEffect mainly defines three effects, which are determined by the enumeration UIBlurEffectStyle, which is defined as follows:

  1. enum UIBlurEffectStyle : Int {
  2. case ExtraLight
  3. case Light
  4. case Dark
  5. }

It mainly determines the blending of the special effect view and the bottom view based on the hue.

Unlike UIBlurEffect, UIVibrancyEffect is mainly used to amplify and adjust the color of the content under the UIVisualEffectView view, while making the content in the contentView of UIVisualEffectView look more vivid. Usually the UIVibrancyEffect object is used together with UIBlurEffect, mainly used to process some display effects on the UIBlurEffect special effect. Following the above code, let's see how to add some new special effects to the blurred view, as shown in the following code:

  1. l et vibrancyView: UIVisualEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: blurEffect))
  2. vibrancyView.setTranslatesAutoresizingMaskIntoConstraints( false )
  3. blurView.contentView.addSubview(vibrancyView)
  4.  
  5. var label: UILabel = UILabel()
  6. label.setTranslatesAutoresizingMaskIntoConstraints( false )
  7. label.text = "Vibrancy Effect"  
  8. label.font = UIFont(name: "HelveticaNeue-Bold" , size: 30 )
  9. label.textAlignment = .Center
  10. label.textColor = UIColor.whiteColor()
  11. vibrancyView.contentView.addSubview(label)

     

The vibrancy effect depends on the color value. All subviews added to contentView must implement the tintColorDidChange method and update themselves. It should be noted that when we use the UIVibrancyEffect(forBlurEffect:) method to create a UIVibrancyEffect, the parameter blurEffect must be the blurEffect we want to add the effect to, otherwise it may not be the effect we want.

In addition, UIVibrancyEffect also provides a class method notificationCenterVibrancyEffect, which is declared as follows:

class func notificationCenterVibrancyEffect() -> UIVibrancyEffect!

This method creates a vibrancy effect for the Today extension in the Notification Center.

refer to

UIVisualEffectView Class Reference

UIVisualEffect Class Reference

UIBlurEffect Class Reference

UIVibrancyEffect Class Reference UIVisualEffect – Swift Tutorial iOS 8: UIVisualEffect

Pointer is missing a nullability type specifier (nonnull or nullable) problem handling— Nullability Annotations

<<:  Different ways to implement locks in Objective-C (Part 2)

>>:  The life cycle of weak

Recommend

Super complete SEM optimization solution! Practical case analysis

This article will teach you how to make an execut...

A three-centimeter-diameter iron rod pierced his head.

Have you ever had such a time, when you have frie...

Experience in operating knowledge accounts with millions of followers on Douyin

2021 is already half over. Where are the opportun...

1600℃! Safely "playing with fire" in the Dream Sky Cabin

Researchers are checking the equipment. Photo pro...

Over 1 billion monthly active users! Check out the advertising in Moments!

On March 21, 2018, Tencent announced its fourth q...

How to use Zhihu for marketing promotion? Zhihu marketing promotion methods!

What is it like for a brand to do marketing on Zh...

Drones are still a niche toy industry or may usher in a blue ocean era

There is a question on Zhihu, an online Q&A c...

Gradle for Android Part 3 (Dependency Management)

Dependency Management Dependency management is wh...