Uivisualeffectview adding special effects to a view

Source: Internet
Author: User

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

In general, to create a special effect, such as a blur effect, you can create a Uivisualeffectview view object that provides a simple way to achieve complex visual effects. This can be seen as a container for effects, and the actual effect affects what is underneath the View object, or what is added to the Contentview of the View object.

Let's take an example to see if using Uivisualeffectview:

1234567 let bgView: UIImageView = UIImageView(image: UIImage(named: "visual"))bgView.frame = self.view.boundsself.view.addSubview(bgView)let blurEffect: UIBlurEffect = UIBlurEffect(style: .Light)let blurView: UIVisualEffectView = UIVisualEffectView(effect: blurEffect)blurView.frame = CGRectMake(50.0, 50.0, self.view.frame.width - 100.0, 200.0)self.view.addSubview(blurView)

This code adds a uiimageview as a background image on the current view controller. The blur effect is then used in a small part of the view.

We can see that the Uivisualeffectview is still very simple. It is important to note that you should not add the child view directly to the Uivisualeffectview view, but should add it to the Contentview of the Uivisualeffectview object.

Also, try to avoid setting the alpha value of the Uivisualeffectview object to a value less than 1.0, because creating a translucent view causes the system to mix the Uivisualeffectview object and all associated sub-views when it is rendered off-screen. This not only consumes CPU/GPU, it can also cause many effects to be displayed incorrectly or not displayed at all.

As we can see above, the method of initializing 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. As we look at the official documentation, we can see that in Uikit, we have defined several specific visual effects, namely Uivisualeffect, Uiblureffect, and Uivibrancyeffect. Their inheritance levels are as follows:

1234 NSObject| -- UIVisualEffect    | -- UIBlurEffect    | -- UIVibrancyEffect

Uivisualeffect is a base class that inherits from NSObject to create visual effects, but this class does not provide any new properties and methods in addition to the properties and methods that inherit from NSObject. Its main purpose is to initialize the Uivisualeffectview, which can be passed into the Uiblureffect or Uivibrancyeffect object in this initialization method.

A Uiblureffect object is used to apply the blur (frosted glass) effect to the content below the Uivisualeffectview view. As shown in the example above. However, the effect of this object does not affect the contents of the Uivisualeffectview object's Contentview.

Uiblureffect primarily defines three effects, which are determined by the enumeration Uiblureffectstyle, which is defined as follows:

12345 enum UIBlurEffectStyle : Int {    caseExtraLight    case Light    caseDark}

It is mainly based on hue (hue) to determine the effect view and the bottom view of the mix.

Unlike Uiblureffect, Uivibrancyeffect is primarily used to magnify and adjust the color of the content below the Uivisualeffectview view, It also makes the content in Uivisualeffectview's contentview look more vivid. Typically, Uivibrancyeffect objects are used with uiblureffect and are primarily used to handle some of the effects on uiblureffect effects. Next to the code above, let's look at the Blur view to add some new effects, as shown in the following code:

12345678910 let vibrancyView: UIVisualEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: blurEffect))vibrancyView.setTranslatesAutoresizingMaskIntoConstraints(false)blurView.contentView.addSubview(vibrancyView)varlabel: UILabel = UILabel()label.setTranslatesAutoresizingMaskIntoConstraints(false)label.text = "Vibrancy Effect"label.font = UIFont(name: "HelveticaNeue-Bold", size: 30)label.textAlignment = .Centerlabel.textColor = UIColor.whiteColor()vibrancyView.contentView.addSubview(label)

Vibrancy effects are dependent on the color value. All child views added to Contentview must implement the Tintcolordidchange method and update themselves. Note that when we use the Uivibrancyeffect (Forblureffect:) method to create the Uivibrancyeffect, the parameter blureffect must be the one we want to add the effect to, Otherwise it may not be the effect we want.

In addition, Uivibrancyeffect provides a class method Notificationcentervibrancyeffect, which declares the following:

1 class func notificationCenterVibrancyEffect() -> UIVibrancyEffect!

This method creates a vibrancy effect for the today extension of the notification hub.

Original link: Use Uivisualeffectview to add special effects to the view

Reference

    1. Uivisualeffectview Class Reference

    2. Uivisualeffect Class Reference

    3. Uiblureffect Class Reference

    4. Uivibrancyeffect Class Reference

    5. Uivisualeffect–swift Tutorial

    6. IOS 8:uivisualeffect

Uivisualeffectview adding special effects to a view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.