This article directory one, add a simple layer two, add a layer to display the picture three, why Calayer use Cgcolorref and cgimageref these 2 types of data, without Uicolor and uiimage. Iv. UIView and Calayer's Options v. Other relations between UIView and Calayer * As mentioned above, UIView internally has a Calayer object (layer) that can be accessed through the Layer property. Note that this default layer does not allow recreating, but you can add child layers to the layer * UIView can add a child view through the Addsubview: method, similarly, Calayer can add a child layer through the Addsublayer: Method next shows how to add a child layer: Back to Top
One, add a simple layer
1 Calayer *mylayer = [Calayer layer];
2//Set width and height of layer (100x100)
3 mylayer.bounds = CGRectMake (0, 0, MB);
4//Set the position of the layer
5 mylayer.position = cgpointmake (MB);
6//Set the background color of the layer: Red
7 Mylayer.backgroundcolor = [Uicolor Redcolor]. Cgcolor;
8//Set layer fillet radius of
9 Mylayer.cornerradius = ten;
11//Add Mylayer to Controller view layer
[Self.view.layer addsublayer:mylayer];
* Line 1th creates an Calayer object that is automatically freed, and you can also use the classic Alloc and Init methods to create
* The 12th line creates a good layer to add to the controller's view layer
Back to top two, add a layer that shows the picture
1 Calayer *mylayer = [Calayer layer];
2//Set width and height of layer (100x100)
3 mylayer.bounds = CGRectMake (0, 0, MB);
4//Set the position of the layer
5 mylayer.position = cgpointmake (MB);
6//Set the picture that needs to be displayed
7 mylayer.contents = (ID) [UIImage imagenamed:@ "Lufy.png"]. Cgimage;
8//Set layer fillet radius of
9 Mylayer.cornerradius = ten;
10//If you set the picture, you need to set this property to Yes to have rounded corner effect one
mylayer.maskstobounds = yes;
13//Add Mylayer to the controller's view layer
[Self.view.layer addsublayer:mylayer];
* In line 7th, set the picture you want to display, note that here is the UIImage Cgimage property, a cgimageref type of data
Back to top three, why Calayer use Cgcolorref and cgimageref in these 2 types of data, instead of Uicolor and UIImage.
* First know: Calayer is defined in the Quartzcore framework; cgimageref, cgcolorref Two types of data are defined in the Coregraphics framework; Uicolor, UIImage is defined in the Uikit framework.
* Second, the Quartzcore framework and coregraphics framework can be used across platforms, both on 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 a specific method of the object, such as UIImage Cgimage method can return a cgimageref
Back to top iv. selection of UIView and Calayer
Careful friend is not difficult to find, in fact, the previous 2 effects can not only be implemented by adding layers, but also by adding uiview to achieve. For example, a 1th red layer can be implemented with a UIView, and the 2nd display image layer can be implemented with a uiimageview. Since both Calayer and UIView can achieve the same display effect, who is the best choice?
* In fact, the contrast Calayer,uiview an event-handling function. In other words, Calayer cannot handle the user's touch events, and UIView can
* So, if the displayed thing needs to interact with the user, use UIView, if you don't need to interact with the user, use UIView or Calayer.
* Of course, the performance of Calayer will be higher, because it is less event-handling function, more lightweight
Back to top five, UIView and Calayer's other relationships
* UIView can access all child views through the Subviews property, and similarly, Calayer can access all of the 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
* Now look at a diagram of UIView and Calayer:
If two uiview are parent-child relationships, their internal calayer is also a parent-child relationship.