There is a default Calayer object layer inside the UIView, although we cannot recreate it, but we can add a sub-layer on top of the door.
We know that UIView has Addsubview: methods, similarly, Calayer also have Addsublayer: methods. Our door can be added from the layer by Addsublayer: again there are layers.
Below we show how to add self-layer.
////VIEWCONTROLLER.M//Cx-calayer (b)////Created by Ma C on 16/3/19.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Initialize LayerCalayer * Layer =[[Calayer alloc]init]; //set the frame of a layerLayer.frame = CGRectMake (0, -, Self.view.frame.size.width, $); //set the background color of a layerLayer.backgroundcolor =[Uicolor Orangecolor]. Cgcolor; //add layer on Self.view.layer[Self.view.layer Addsublayer:layer]; }@end
In addition to this simple layer, we can also set the layer of the picture. (layer.contents = (ID) [UIImage imagenamed:@ "Nvshen.jpg"]. Cgimage;)
////VIEWCONTROLLER.M//Cx-calayer (b)////Created by Ma C on 16/3/19.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Initialize LayerCalayer * Layer =[[Calayer alloc]init]; //set the frame of a layerLayer.frame = CGRectMake (0, -, Self.view.frame.size.width, self.view.frame.size.height); //Add a pictureLayer.contents = (ID) [UIImage imagenamed:@"nvshen.jpg"]. Cgimage; //set the background color of a layerLayer.backgroundcolor =[Uicolor Orangecolor]. Cgcolor; //add layer on Self.view.layer[Self.view.layer Addsublayer:layer]; }@end
The following is a reference to MJ God's explanation:
Why use the 2 data types Cgcolorref and Cgimageref in Calayer instead of Uicolor and uiimage?
* First of all to know: Calayer is defined in the Quartzcore framework, CGIMAGEREF, cgcolorref two data types are defined in the Coregraphics framework; Uicolor, UIImage is defined in the Uikit frame
* Second, the Quartzcore framework and the Coregraphics framework are available across platforms and can be used on both iOS and Mac OS x, but Uikit can only be used in iOS
* Therefore, in order to ensure portability, quartzcore can not use UIImage, Uicolor, can only use Cgimageref, Cgcolorref
* However, in many cases, the Coregraphics object can be obtained by Uikit the specific method of the object, such as UIImage Cgimage method can return a cgimageref
Choice of UIView and Calayer
* In fact, compared with the Calayer,uiview more than one event processing function. In other words, Calayer cannot handle user touch events, and UIView can
* So, if the displayed things need to interact with the user, with UIView, if you do not need to interact with the user, with UIView or Calayer can
* Of course, calayer performance will be higher because it is less event-handling, more lightweight
Other relationships between UIView and Calayer
* UIView can access all sub-views through the Subviews property, similarly, Calayer can access all child layers through the Sublayers property
* UIView can access the parent view through the Superview property, similarly, Calayer can also access the parent layer through the Superlayer property
* See below for a diagram of UIView and Calayer:
If two UIView is a parent-child relationship, then the Calayer within them is also a parent-child relationship.
IOS Calayer (ii)