directory of this document
- One, add a simple layer
- Second, add a layer to display the picture
- Third, why Calayer use Cgcolorref and cgimageref these 2 data types, instead of Uicolor and uiimage?
- Iv. selection of UIView and Calayer
- V. Other relations of UIView and Calayer
* As mentioned above, UIView has a Calayer object (layer) on the inside, which can be accessed through the Layer property. Note that this default layer does not allow re-creation, but you can add a child layer inside the layer * UIView can add a child view through the Addsubview: method, similarly, Calayer can be addsublayer: Method Add Sublayer Next, show how to add a child layer: go back to the top and add a simple layer
1 Calayer *mylayer = [Calayer layer]; 2//Set the width and height of the layer (100x100) 3 mylayer.bounds = CGRectMake (0, 0, 100, 100); 4//Set the position of the layer 5 mylayer.position = cgpointmake (100, 100); 6//Set the background color of the layer: Red 7 mylayer.backgroundcolor = [Uicolor Redcolor]. Cgcolor; 8//Set the fillet radius of 9 Mylayer.cornerradius = 10;10 11//Add Mylayer to the view layer of the controller [Self.view.layer Addsublayer:mylayer];
* Line 1th creates an automatically released Calayer object, and you can also use the classic Alloc and Init methods to create
* The 12th row will be created to add the layer to the view layer of the Controller
Back to top two, add a layer to display the picture
1 Calayer *mylayer = [Calayer layer]; 2//Set the width and height of the layer (100x100) 3 mylayer.bounds = CGRectMake (0, 0, 100, 100); 4//Set the position of the layer 5 mylayer.position = cgpointmake (100, 100); 6//Set the picture that needs to be displayed 7 mylayer.contents = (ID) [UIImage imagenamed:@ "Lufy.png"]. Cgimage; 8//Set the fillet radius of 9 Mylayer.cornerradius = 10;10//If the picture is set, you need to set this property to YES to have rounded effect one-mylayer.maskstobounds = yes;12 13//Add Myl Ayer to the layer of the controller's view [Self.view.layer Addsublayer:mylayer];
* Set the picture to be displayed on line 7th, note that the UIImage cgimage attribute is used here and is a cgimageref type of data
Back to the top three, why Calayer use Cgcolorref and cgimageref these 2 types of data, 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
Go back to the top four, UIView and Calayer options
Careful friend is not difficult to find, in fact, the previous 2 effects can be achieved not only by adding layers, but also by adding uiview to achieve. For example, the 1th red layer can be implemented with a UIView, and the 2nd layer showing the image can be implemented with a uiimageview. Since Calayer and UIView all can achieve the same display effect, then who should choose who is good?
* 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
Back to the top five, UIView and calayer other relationships
* 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.
calayer2-Creating a new layer