One, add a layer
Steps to add a layer:
1. Create Layer2. Set the properties of the layer (color is set, bounds can be displayed) 3. Add a layer to the interface (on the layer of the Controller view)
1//2//Yyviewcontroller.m3//01-Create a simple layer4//5//Created by Apple on 14-6-21.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"1011@interfaceYyviewcontroller ()1213@end1415@implementationYyviewcontroller1617-(void) Viewdidload18{19[Super Viewdidload];2021st//1. Create a layer22//Using object methods to create23//Calayer *layer=[[calayer Alloc]init];24//Use the class method to createCalayer *layer=[Calayer layer];2627//2. Set the properties of the layer (be sure to set the location for the color properties to be displayed)Layer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;Layer.bounds=cgrectmake (0, 0, $ ); (Layer.position=cgpointmake ); 3. Add layer to interface [Self.view.layer Addsublayer:layer]; @end
Second, add a layer to display the picture
code example:
1//2//Yyviewcontroller.m3//02-Add a layer showing the picture4//5//Created by Apple on 14-6-21.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"1011@interfaceYyviewcontroller ()1213@end1415@implementationYyviewcontroller1617-(void) Viewdidload18{19[Super Viewdidload];20//Create a layerCalayer *mylayer=[Calayer layer];22//Set the properties of a layerMylayer.bounds=cgrectmake (100,100,100,100);Mylayer.position=cgpointmake (100,100);2526//Set up a picture to displayMylayer.contents= (ID) [UIImage imagenamed:@"Me"]. Cgimage;28//Set the fillet radius to 10mylayer.cornerradius=10;30//31 Mylayer.maskstobounds=yes; // set border 33 Mylayer.borderwidth=3; Mylayer.bordercolor=[uicolor Browncolor]. Cgcolor; 35 36 // add layer to interface 37 [Self.view.layer Addsublayer:mylayer ]; 38 }39 @end
Execution effect:
Description: In line 27th set the picture to be displayed, note that the UIImage cgimage attribute is used here, is a cgimageref type of data
Third, cgcolorref and cgimageref data types
1. Brief description
Calayer is defined in the Quartzcore framework; cgimageref, cgcolorref two data types are defined in the Coregraphics framework; Uicolor, uiimage are defined in the Uikit framework.
Second, theQuartzcore 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
2. Import the Quartzcore framework
It is no longer necessary for us to import this framework in XCODE5, if we need to import the framework before IOS6 and XCODE4
(1) Click on the project name and click on the right targets below target
(2) After clicking on Build pases, expand link Binary ...., add + sign
(3) Enter "Quartz" in the search box, check quartzcore.framework, and add
(4) Once added, this frame will appear in the project folder
Finally, you need to import the framework's main header file in your project code.
#import <QuartzCore/QuartzCore.h>
Iv. selection of UIView and Calayer
You can see that the previous 2 effects can be implemented not only by adding layers, but also by adding uiview. The layer that displays the picture 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, while UIView can.
Therefore, in the process of selection, you need to take into account the actual situation, 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, the performance of Calayer is higher because it is less capable of event handling and more lightweight
V. Additional Information
1. Adding a child layer
1#import"YYViewController.h"23@interfaceYyviewcontroller ()45@end67@implementationYyviewcontroller89-(void) Viewdidload10{11[Super Viewdidload];12NSLog (@"star-%@", self.view.layer.sublayers);14//1. Create a layer15//Using object methods to create16//Calayer *layer=[[calayer Alloc]init];17//Use the class method to createCalayer *layer=[Calayer layer];1920//2. Set the properties of the layer (be sure to set the location for the color properties to be displayed)Layer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;Layer.bounds=cgrectmake (0, 0, 200, 200); 23 layer.position=cgpointmake (100, 100); 24 25 //3. Adding a layer to the interface 26 [Self.view.layer Addsublayer:layer ]; 27 28 NSLog (@ "end-%@ "29}
The printing results are as follows:
Note: Before adding a layer, the Controller view layer has two sub-layers, and after adding, there are three sub-layers.
2. Access Layer
1-(void) Viewdidload2{3[Super Viewdidload];45 Calayer *layer=[Calayer layer];6 layer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;7 Layer.bounds=cgrectmake (0,0,200,200);8 Layer.position=cgpointmake (100,100);910[Self.view.layer Addsublayer:layer];11//Calayer access to all child layers through the Sublayers propertyNSLog (@"%@", self.view.layer.sublayers[2]); NSLog (@ "%@", layer); calayer//can also access the parent layer (@ "%@", Layer.superlayer) via the Superlayer property NSLog NSLog (@ "%@", Self.view.layer); (+}
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
3. Note
In storyboard, drag a button on the interface to print the number of child layers.
1-(void) Viewdidload2{3[Super Viewdidload];45 Calayer *layer=[Calayer layer];6 layer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;7 Layer.bounds=cgrectmake (0, 0, Ten, 8 Layer.position=cgpointmake (+), 9 [ Self.view.layer Addsublayer:layer]; one // print all layerNSLog (@ "%@", self.view.layer.sublayers); -}
The printing results are as follows:
Special NOTE: If one control is a child of another control, the layer of the control is also a child layer of the other control.
iOS Development UI Chapter-calayer (Create Layer)