iOS development--graphics programming oc & (v) Calayer Creating layers
Last Update:2015-06-04
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Calayer (create Layer)</strong></p></p><p><p><strong>one, Add a layer</strong></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Steps to add a layer:</p></p>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)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #008000;">//<span style="color: #008080;">2<span style="color: #008000;">//<span style="color: #008000;">Yyviewcontroller.m<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">01-create a simple layer<span style="color: #008080;">4<span style="color: #008000;">//<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Created by Apple on 14-6-21.<span style="color: #008080;">6<span style="color: #008000;">//<span style="color: #008000;">Copyright (c) 2014 Itcase. All Rights Reserved.<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008080;">8<span style="color: #008080;">9<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYViewController.h<span style="color: #800000;">"<span style="color: #008080;">10<span style="color: #008080;">11<span style="color: #0000ff;">@interface<span style="color: #000000;">Yyviewcontroller ()<span style="color: #008080;">12<span style="color: #008080;">13<span style="color: #0000ff;">@end<span style="color: #008080;">14<span style="color: #008080;">15<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyviewcontroller<span style="color: #008080;">16<span style="color: #008080;">17-(<span style="color: #0000ff;">void<span style="color: #000000;">) Viewdidload<span style="color: #008080;">18<span style="color: #000000;">{<span style="color: #008080;">19<span style="color: #000000;">[super viewdidload];<span style="color: #008080;">20<span style="color: #008080;">21st<span style="color: #008000;">//<span style="color: #008000;">1. Create a layer<span style="color: #008080;">22<span style="color: #008000;">//<span style="color: #008000;">Using object methods to create<span style="color: #008080;">23<span style="color: #008000;">//<span style="color: #008000;">Calayer *layer=[[calayer alloc]init];<span style="color: #008080;">24<span style="color: #008000;">//<span style="color: #008000;">Use the class method to create<span style="color: #008080;">Calayer *layer=<span style="color: #000000;">[calayer layer];<span style="color: #008080;">26<span style="color: #008080;">27<span style="color: #008000;">//<span style="color: #008000;">2. Set the properties of the layer (be sure to set the location for the color properties to be displayed)<span style="color: #008080;">Layer.backgroundcolor=<span style="color: #000000;">[uicolor browncolor]. cgcolor;<span style="color: #008080;"><span style="color: #008080;">Layer.bounds=cgrectmake (<span style="color: #800080;">0, <span style="color: #800080;">0, $ <span style="color: #800080;"> <span style="color: #800080;"><span style="color: #000000;">); <span style="color: #008080;"> (layer.position=cgpointmake<span style="color: #800080;"> <span style="color: #800080;"><span style="color: #000000;">); <span style="color: #008080;"> <span style="color: #008080;"> <span style="color: #008000;"><span style="color: #008000;">3. Add layer to interface <span style="color: #008080;"> <span style="color: #000000;">[self.view.layer addsublayer:layer]; <span style="color: #008080;"> <span style="color: #000000;"><span style="color: #008080;"> <span style="color: #008080;"> <span style="color: #0000ff;">@end</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p> </p></p><p><p></p></p><p><p><strong>second, add a layer to display the picture</strong></p></p><p><p><strong>Code example:</strong></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #008000;">//<span style="color: #008080;">2<span style="color: #008000;">//<span style="color: #008000;">Yyviewcontroller.m<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">02-add a layer showing the picture<span style="color: #008080;">4<span style="color: #008000;">//<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Created by Apple on 14-6-21.<span style="color: #008080;">6<span style="color: #008000;">//<span style="color: #008000;">Copyright (c) 2014 Itcase. All Rights Reserved.<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008080;">8<span style="color: #008080;">9<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYViewController.h<span style="color: #800000;">"<span style="color: #008080;">10<span style="color: #008080;">11<span style="color: #0000ff;">@interface<span style="color: #000000;">Yyviewcontroller ()<span style="color: #008080;">12<span style="color: #008080;">13<span style="color: #0000ff;">@end<span style="color: #008080;">14<span style="color: #008080;">15<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyviewcontroller<span style="color: #008080;">16<span style="color: #008080;">17-(<span style="color: #0000ff;">void<span style="color: #000000;">) Viewdidload<span style="color: #008080;">18<span style="color: #000000;">{<span style="color: #008080;">19<span style="color: #000000;">[super viewdidload];<span style="color: #008080;">20<span style="color: #008000;">//<span style="color: #008000;">Create a layer<span style="color: #008080;">Calayer *mylayer=<span style="color: #000000;">[calayer layer];<span style="color: #008080;">22<span style="color: #008000;">//<span style="color: #008000;">Set the properties of a layer<span style="color: #008080;">Mylayer.bounds=cgrectmake (<span style="color: #800080;">100,<span style="color: #800080;">100,<span style="color: #800080;">100,<span style="color: #800080;">100<span style="color: #000000;">);<span style="color: #008080;">Mylayer.position=cgpointmake (<span style="color: #800080;">100,<span style="color: #800080;">100<span style="color: #000000;">);<span style="color: #008080;">25<span style="color: #008080;">26<span style="color: #008000;">//<span style="color: #008000;">Set up a picture to display<span style="color: #008080;">Mylayer.contents= (<span style="color: #0000ff;">Id) [UIImage imagenamed:<span style="color: #800000;">@"<span style="color: #800000;">Me<span style="color: #800000;">"<span style="color: #000000;">]. cgimage;<span style="color: #008080;">28<span style="color: #008000;">//<span style="color: #008000;">Set the fillet radius to 10<span style="color: #008080;">mylayer.cornerradius=<span style="color: #800080;">10<span style="color: #000000;">;<span style="color: #008080;">30<span style="color: #008000;">//<span style="color: #008000;"><span style="color: #008080;" if you set the picture, need to this property yes show fillet effect <span>31 Mylayer.maskstobounds=<span style="color: #000000;">yes;<span style="color: #008080;"> <span style="color: #008000;">//<span style="color: #008000;"> set border <span style="color: #008080;">33 Mylayer.borderwidth=<span style="color: #800080;">3<span style="color: #000000;">;<span style="color: #008080;"> Mylayer.bordercolor=<span style="color: #000000;">[uicolor browncolor]. cgcolor; <span style="color: #008080;">35 <span style="color: #008080;">36 <span style="color: #008000;">//<span style=" Color: #008000; "> add layer to interface <span style="color: #008080;">37 <span style="color: #000000;"> [self.view.layer Addsublayer:mylayer ]; <span style="color: #008080;">38 <span style="color: #000000;">}<span style="color: #008080;">39 <span style=" Color: #0000ff; "> @end </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 30px;"><p style="margin-left: 30px;">Execution Effect:</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"><span>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</span></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p><strong>third,</strong> <strong>cgcolorref and cgimageref data types</strong></p></p><p><p>1. Brief description</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">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.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">second, the<span style="color: #ff0000;">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</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">therefore, in order to ensure portability, Quartzcore can not use uiimage, uicolor, can only use cgimageref, cgcolorref</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">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</p></p><p><p>2. Import the Quartzcore framework</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">It is no longer necessary for us to import this framework in xcode5, if we need to import the framework before IOS6 and XCODE4</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(1) Click on the project name and click on the right targets below target</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(2) after clicking on Build pases, expand link Binary ...., Add + sign</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(3) Enter "Quartz" in the search box, check quartzcore.framework, and add</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(4) once added, this frame will appear in the project folder</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"> </p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">finally, you need to import the Framework's main header file in your project Code.</p></p><pre><pre><span style="color: #0000ff;">#import <QuartzCore/QuartzCore.h></span></pre></pre><p><p></p></p><p><p><strong>Iv. selection of UIView and Calayer</strong></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">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?</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">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.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">therefore, in the process of selection, you need to take into account the actual situation, <span style="color: #ff0000;">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</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">of course, the performance of Calayer is higher because it is less capable of event handling and more lightweight</p></p><p><p><strong>V. Additional Information</strong></p></p><p><p>1. Adding a child layer</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYViewController.h<span style="color: #800000;">"<span style="color: #008080;">2<span style="color: #008080;">3<span style="color: #0000ff;">@interface<span style="color: #000000;">Yyviewcontroller ()<span style="color: #008080;">4<span style="color: #008080;">5<span style="color: #0000ff;">@end<span style="color: #008080;">6<span style="color: #008080;">7<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyviewcontroller<span style="color: #008080;">8<span style="color: #008080;">9-(<span style="color: #0000ff;">void<span style="color: #000000;">) Viewdidload<span style="color: #008080;">10<span style="color: #000000;">{<span style="color: #008080;">11<span style="color: #000000;">[super viewdidload];<span style="color: #008080;">12<span style="color: #008080;">NSLog (<span style="color: #800000;">@"<span style="color: #800000;">star-%@<span style="color: #800000;">"<span style="color: #000000;">, self.view.layer.sublayers);<span style="color: #008080;">14<span style="color: #008000;">//<span style="color: #008000;">1. Create a layer<span style="color: #008080;">15<span style="color: #008000;">//<span style="color: #008000;">Using object methods to create<span style="color: #008080;">16<span style="color: #008000;">//<span style="color: #008000;">Calayer *layer=[[calayer alloc]init];<span style="color: #008080;">17<span style="color: #008000;">//<span style="color: #008000;">Use the class method to create<span style="color: #008080;">Calayer *layer=<span style="color: #000000;">[calayer layer];<span style="color: #008080;">19<span style="color: #008080;">20<span style="color: #008000;">//<span style="color: #008000;">2. Set the properties of the layer (be sure to set the location for the color properties to be displayed)<span style="color: #008080;">Layer.backgroundcolor=<span style="color: #000000;">[uicolor browncolor]. cgcolor;<span style="color: #008080;">Layer.bounds=cgrectmake (<span style="color: #800080;"><span style="color: #800080;">0, <span style="color: #800080;">0, <span style="color: #800080;">200, <span style=" Color: #800080; ">200<span style="color: #000000;">); <span style="color: #008080;">23 layer.position=cgpointmake (<span style="color: #800080;">100, <span style="color : #800080; ">100<span style="color: #000000;">); <span style="color: #008080;">24 <span style="color: #008080;">25 <span style="color: #008000;">//<span style=" Color: #008000; ">3. adding a layer to the interface <span style="color: #008080;">26 <span style="color: #000000;"> [self.view.layer Addsublayer:layer ]; <span style="color: #008080;">27 <span style="color: #008080;">28 NSLog (<span style="color: #800000;">@ "<span style="color: #800000;">end-%@<span style="color: #800000;"> "<span style=" color: #000000; >,self.view.layer.sublayers); <span style= " color: #008080; ">29} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 30px;"><p style="margin-left: 30px;">The printing results are as follows:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span>Note: before adding a layer, The Controller view layer has two sub-layers, and after adding, there are three Sub-layers.</span></p></p><p><p>2. Access Layer</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1-(<span style="color: #0000ff;">void<span style="color: #000000;">) Viewdidload<span style="color: #008080;">2<span style="color: #000000;">{<span style="color: #008080;">3<span style="color: #000000;">[super viewdidload];<span style="color: #008080;">4<span style="color: #008080;">5 Calayer *layer=<span style="color: #000000;">[calayer layer];<span style="color: #008080;">6 layer.backgroundcolor=<span style="color: #000000;">[uicolor browncolor]. cgcolor;<span style="color: #008080;">7 Layer.bounds=cgrectmake (<span style="color: #800080;">0,<span style="color: #800080;">0,<span style="color: #800080;">200,<span style="color: #800080;">200<span style="color: #000000;">);<span style="color: #008080;">8 Layer.position=cgpointmake (<span style="color: #800080;">100,<span style="color: #800080;">100<span style="color: #000000;">);<span style="color: #008080;">9<span style="color: #008080;">10<span style="color: #000000;">[self.view.layer addsublayer:layer];<span style="color: #008080;">11<span style="color: #008000;">//<span style="color: #008000;">Calayer access to all child layers through the Sublayers property<span style="color: #008080;">NSLog (<span style="color: #800000;">@"<span style="color: #800000;">%@<span style="color: #800000;">", self.view.layer.sublayers[<span style="color: #800080;"><span style="color: #800080;">2<span style="color: #000000;">]); <span style="color: #008080;">NSLog (<span style="color: #800000;">@ "<span style="color: #800000;">%@<span style="color: #800000;">"<span style="color: #000000;">, layer); <span style="color: #008080;"> <span style="color: #008080;"> <span style="color: #008000;">calayer//<span style="color: #008000;">can also access the parent layer <span style="color: #008080;">(<span style="color: #800000;">@ "<span style="color: #800000;">%@<span style="color: #800000;">"<span style="color: #000000;">, Layer.superlayer) <span style="color: #008080;">via the Superlayer property NSLog NSLog (<span style="color: #800000;">@ "<span style="color: #800000;">%@<span style="color: #800000;">"<span style="color: #000000;">, self.view.layer); <span style="color: #008080;"> </span></span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> (+} </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p>UIView can access all sub-views through the subviews property, similarly, Calayer can access all child layers through the Sublayers property</p></p><p><p>UIView can access the parent view through the Superview property, similarly, Calayer can also access the parent layer through the Superlayer property</p></p><p><p>3. Note</p></p><p><p>In storyboard, drag a button on the interface to print the number of child Layers.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1-(<span style="color: #0000ff;">void<span style="color: #000000;">) Viewdidload<span style="color: #008080;">2<span style="color: #000000;">{<span style="color: #008080;">3<span style="color: #000000;">[super viewdidload];<span style="color: #008080;">4<span style="color: #008080;">5 Calayer *layer=<span style="color: #000000;">[calayer layer];<span style="color: #008080;">6 layer.backgroundcolor=<span style="color: #000000;">[uicolor browncolor]. cgcolor;<span style="color: #008080;">7 Layer.bounds=cgrectmake (<span style="color: #800080;"><span style="color: #800080;">0, <span style="color: #800080;">0, <span style="color: #800080;">ten, <span style="color: #800080;"><span style="color: #000000;"><span style="color: #008080;"> 8 layer.position=cgpointmake (+),<span style="color: #800080;"> <span style="color: #800080;"><span style="color: #000000;"><span style="color: #008080;"> 9 <span style="color: #008080;"> <span style="color: #000000;">[ Self.view.layer addsublayer:layer]; <span style="color: #008080;">one <span style="color: #008000;">// <span style="color: #008000;">print all layer<span style="color: #008080;">NSLog (<span style="color: #800000;">@ "<span style="color: #800000;">%@<span style="color: #800000;">"<span style="color: #000000;">, self.view.layer.sublayers); <span style="color: #008080;"> -}</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 30px;"><p style="margin-left: 30px;">The printing results are as follows:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span>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.</span></p></p><p><p>iOS development--graphics programming oc & (v) Calayer Creating layers</p></p></span>