iOS development - the difference between UIView attributes hidden, opaque, alpha, opacity

iOS development - the difference between UIView attributes hidden, opaque, alpha, opacity

1. alpha

The liquid crystal display is composed of pixels, and each pixel can display a color value composed of the RGBA color space. Among them, A represents transparency alpha, and alpha in UIView is a floating-point value, ranging from 0 to 1.0, which means from completely transparent to completely opaque.
1, alpha will affect its own transparency, but also affect the transparency of the subView.
2. After alpha is set to empty, UIView will not be removed from the responder chain and can still receive events.
3. Change the alpha, which is animated by default. This is because the layer is represented by CALayer in Core Animation in Cocoa, and the animation effect is the implicit animation of CALayer. Of course there is also a way to disable this animation effect.
 
 

二、hidden

This property is a BOOL value, which is used to indicate whether the UIView is hidden. The default value is NO.
When the value is set to YES:
1. The current UIView and subview will be hidden, regardless of the hidden value of the subview.
2. The current UIView will be removed from the responder chain, and the next one in the responder chain will become the first responder
 

Three, alpha - hidden comparison

1. Impact on subView: alpha will affect the transparency of the subView, and hidden will also affect the subView
2. Hide UIView: Setting alpha to 0 can hide UIView, and setting hidden to YES can also hide UIVIew, the effect is the same, and the performance of using hidden is higher.
 

Four, opaque

This property is a BOOL value. The default value of UIView is YES, but the default value of UIButton and other subclasses is NO. opaque indicates whether the current UIView is opaque, but the funny thing is that it doesn't actually determine whether the current UIView is opaque or not. For example, if you set opaque to NO, the UIView is still visible. Its function is to provide a performance optimization switch for the drawing system. If the value is YES, the drawing treats the entire view as opaque when drawing the view. In this way, the drawing system will optimize some operations and provide system performance during the drawing process; if it is set to NO, the drawing system will treat it and other content equally, and will not do optimization operations. For performance reasons, it is set to YES by default (meaning optimized).
1. When UIView has a background color: and the background color has transparency (when the transparency is not 1), setting opaque to YES has higher performance.
2. When UIVIew has a background color: and the transparency of the background color is 1, the value of opaque does not affect the performance.
3. When UIVIew has no background color: the value of opaque does not affect performance.
 

Five, opacity - properties of CALayer

Opacity in CALayer is a floating-point value, ranging from 0 to 1.0, which means from completely transparent to completely opaque.
1. Opacity will affect its own transparency, as well as the transparency of subLayer.
2. After the opacity is set to empty, CALayer will not be removed from the responder chain, and events can still be received.
 

Six, how to set UIView transparent, subView opaque

1. Use a translucent image as the background.
This method can meet the requirements, but the project should add as few resources as possible, and try not to use pictures if you can.
2. Use the colorWithWhite:alpha: method
view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
This is also a method that uses more. The parameter behind white represents the grayscale, and the change from black to white is between 0 and 1. alpha is the transparency that you want to adjust.
3. Use the colorWithRed:green:blue:alpha: method
Setting alpha is the transparency you want to adjust.
4. Set the background in xib or storyBoard
Translucent background xib settings.png
Adjust the transparency by setting the opacity slider in the background color settings diagram.
5. Use the colorWithAlphaComponent: method
This is an instance method, an instance of UIColor will return a UIColor with transparency after calling this method. The usage method is as follows:
UIColor *color = [UIColor blackColor];
bgView.backgroundColor = [color colorWithAlphaComponent:0.5];

 









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324967604&siteId=291194637