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

Li Liheng Everyone is a Sales Champion Course

Course Description: Li Liheng 10-year veteran in ...

Interpreting the “Ten Articles of WeChat”, what changes have they brought?

At present, instant messaging tools represented b...

A review of the top ten public relations crisis events in 2018 (Part 1)

Today, I will use three crisis public relations t...

Night Grass and Chollima 7-day Case Study Practice Training Camp 17th

Resource introduction of the 17th issue of the Ni...

How to quickly attract “new customers” and “new fans”?

Time is money, so relying solely on long-termism ...

A moldy cantaloupe began to change the fate of mankind...

The discovery of penicillin is probably the most ...

Is it true that it will rain during Qingming Festival?

Audit expert: Zhan Mingjin PhD, Chinese Academy o...

The battle between the two: a game of efficiency

What is the most essential meaning of competition...

The history of the battle between elephant ghosts and machines

© wikimedia Leviathan Press: I personally have al...

Where can I buy the Black Blessing video?

Video production of African children holding sign...

An inch longer is an inch stronger. How can you stretch to be healthier?

There is an old saying that "an inch longer ...