IOS CALayer (2), ioscalayer
UIview has a default CALayer Object layer. Although we cannot re-create it, we can add sub-layers to it.
We know that UIView has addSubview: method. Similarly, CALayer also has addSublayer: method. You can use addSublayer to add a self-layer to an existing layer.
Next, we will demonstrate how to add a layer.
/// ViewController. m // CX-CALayer (2) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // initialize layer CALayer * layer = [[CALayer alloc] init]; // set the frame layer of the layer. frame = CGRectMake (0, 20, self. view. frame. size. width, 200); // sets the background color of the layer. backgroundColor = [UIColor orangeColor]. CGColor; // then self. view. add layer [self. view. layer addSublayer: layer];} @ end
In addition to this simple layer, we can also set the layers of the image. (Layer. contents = (id) [UIImage imageNamed: @ "nvshen.jpg"]. CGImage ;)
/// ViewController. m // CX-CALayer (2) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // initialize layer CALayer * layer = [[CALayer alloc] init]; // set the frame layer of the layer. frame = CGRectMake (0, 20, self. view. frame. size. width, self. view. frame. size. height); // Add an image layer. contents = (id) [UIImage imageNamed: @ "nvshen.jpg"]. CGImage; // set the background color of the layer. backgroundColor = [UIColor orangeColor]. CGColor; // then self. view. add layer [self. view. layer addSublayer: layer];} @ end
The following describes the introduction of MJ:
Why does CALayer use CGColorRef and CGImageRef data types instead of UIColor and UIImage?
* First, you must know that CALayer is defined in the QuartzCore framework; CGImageRef and CGColorRef are defined in the CoreGraphics framework; UIColor and UIImage are defined in the UIKit framework.
* Second, The QuartzCore and CoreGraphics frameworks can be used across platforms, both iOS and Mac OS X, but UIKit can only be used in iOS.
* To ensure portability, QuartzCore cannot use UIImage or UIColor. Only CGImageRef and CGColorRef can be used.
* In many cases, the CoreGraphics object can be obtained through a specific method of the UIKit object. For example, the CGImage method of UIImage can return a CGImageRef
UIView and CALayer Selection
* In fact, compared with CALayer, UIView has an additional event processing function. That is to say, CALayer cannot process user touch events, while UIView can
* If the displayed items need to interact with users, use UIView. If you do not need to interact with users, use UIView or CALayer.
* Of course, CALayer has higher performance because it lacks the event processing function and is more lightweight.
Other relationships between UIView and CALayer
* UIView can access all child views through the subviews attribute. Similarly, CALayer can also access all child layers through the sublayers attribute.
* UIView can access the parent view through the superview attribute. Similarly, CALayer can also access the parent layer through the superlayer attribute.
* Next, let's look at the relationship between UIView and CALayer:
If two uiviews have parent-child relationships, the internal CALayer is also a parent-child relationship.