The Calayer of advanced techniques in iOS core animation (i)

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color: #ff0000; font-size: 16px;">The relationship between UIView and Calayer</span><br><span style="font-size: 16px;">In ios, a uiview corresponds to a calayer, and the View's responsibility is to create and manage this layer to ensure that when the sub-view is added to or removed from the hierarchical relationship, The layer they are associated with also corresponds to the same action in the hierarchy Tree. in fact, the layers behind these are really used to display and animate on the screen, and UIView is just a package for it, providing some specific features that iOS is similar to touch, and the core Animation advanced interface for the underlying METHOD. (calayer does not respond to events, it provides several areas of the layer that can be judged by a single contact.)</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;">The significance of the existence of Calayer</span><br><span style="font-size: 16px;">The reason is to provide calayer and uiview these two parallel levels, on the one hand this can do separation of duties, can avoid a lot of duplicate code, on the other hand, iOS and Mac OS interface actually not much difference, But because the iOS multi-touch user interface and Mac are fundamentally different based on the mouse keyboard, a common calayer is provided to provide an interface that provides UIView and NSView to provide events</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;">Use of Calayer</span><br><span style="font-size: 16px;">Usually we use UIView in this way:</span></p></p><pre><pre><span style="font-size: 14px;">1 Let Blueview = UIView (frame:cgrectmake (3,,)) 2 blueview.backgroundcolor = Uicolor.bluecolor (), self.view. Addsubview (blueview)</span></pre></pre><p><p><span style="font-size: 14px;">  <span style="font-size: 16px;">and we can use calayer, you can directly use the following paragraph directly replace the above code, the program does not have any difference in appearance</span></span></p></p><pre><pre><span style="font-size: 14px;">1 Let Bluelayer = Calayer () 2 bluelayer.frame = CGRectMake (+,, 3) bluelayer.backgroundcolor = Uicolor.bluecol or (). CGColor4 Self.view.layer.addSublayer (bluelayer)</span></pre></pre><p><p><span style="color: #ff0000; font-size: 16px;">Calayer content (contents) related properties</span><br><span style="color: #339966; font-size: 16px;">Contents property</span><br><span style="font-size: 16px;">The layer has a contents property, which needs to pass in an ID (anyobject!) type, this is because it needs cgimage on the iOS platform and the Mac needs nsimage, in OC you need to use the ID type to turn a bit, in swift you just need to assign a cgimage directly, Because any class type of object can be assigned to anyobject, if you pass in other objects, the program will not error, but the picture will not be displayed, Uiimageview can display the picture inside is also used this contents property for the sake of</span></p></p><p><p><span style="color: #339966; font-size: 16px;">Contentgravity Property</span><br><span style="font-size: 16px;">The Contentgravity property corresponds to the View's contentmode property, which controls how the layer corresponds and stretches, although its value is a string, but Swift helps it provide a constant string that corresponds to each string.</span></p></p><p><p><span style="color: #339966; font-size: 16px;">Contentsscale Property</span><br><span style="font-size: 16px;">The Contentsscale property is corresponding to the contentscalefactor in the uiview, which determines the scale of a picture and view, that is, a point on the screen that shows a few pixels, which is the principle of the screen adaptation of the iOS device, a uiimage that contains scales, direction and other information, and converted to Cgimage will lose this information, you can use the Contentsscale property to set Image.scale to it.</span></p></p><p><p><span style="color: #339966; font-size: 16px;">Masktobounds Property</span><br><span style="font-size: 16px;">The Masktobounds property corresponds to the Maskstobounds property of calayer, and if set to true, the external is clipped</span></p></p><p><p><span style="color: #339966; font-size: 16px;">Contentsrect Property</span><br><span style="font-size: 16px;">The Contentsrect property allows us to display a part of the image in the layer, the clipping area of this image is this property, it is a cgrect, using it you can do picture flattening (that is, a set of pictures in a picture, and then cut the image of processing and then use), so in memory use/ Loading time/rendering performance has advantages, its value is proportional, the maximum is 1.</span></p></p><p><p><span style="color: #339966; font-size: 16px;">Contentscenter Property</span><br><span style="font-size: 16px;">The Contentscenter property corresponds to Uiimage's resizableimagewithcapinsets, and its value is a cgrect, which represents the magnified area, and its effect is when the party Contentscale magnifies, Only enlarge Contentscenter area, other area compress</span></p></p><p><p> <span style="color: #ff0000; font-size:16px;"> iOS drawing </span> <br> <span style="font-size:16px;" cgimage is not the only one that can be assigned to contents property, or it drawn with core graphics, if you implement drawrect method, and then call setneedsdisplay appearance property changed, it will automatically auto redraw, although drawret a uiview but in fact, all bottom calayer redraw saved picture, do need customize painting write an empty drawrect consumes cpu memory resources < span> <br> <span style="font-size:16px;">calayer has an optional delegate property, if delegate is set, and the Displey method of the layer is actively invoked ( Note that unlike drawrect, the redraw timing is the Developer's own control, and the Setneedsdisplay method can be called to call the system itself, and it calls Displaylayer (layer:calayer!) method, here is the last chance to set the contents property, if you do not implement this method, it will try to call the following method: Drawlayer (layer:calayer!,incontext ctx:cgcontext!), If you implement the Displaylayer method, the following method will not be called, Drawlayer This method you can do the drawing </span> </span></p></p><pre><pre><span style="font-size: 14px;">1 override func Drawlayer (layer:calayer!, incontext ctx:cgcontext!) {2 Cgcontextsetlinewidth (ctx, 10.0) 3 cgcontextsetstrokecolorwithcolor (ctx, uicolor.redcolor (). cgcolor); 4 cgcontextstrokeellipseinrect (ctx, layer.bounds) d0605579435}6 override func Displaylayer (layer: Calayer!) {7 layer.contents = UIImage (named: "11.png")?. CGImage8}</span></pre></pre><p><p><span style="font-size: 16px;">Because of a uiview it will set its corresponding Calayer delegate to its own, so you can no longer set the other layer of the delegate for it, in UIView with DrawRect method, The use of delegate can only be used on a separate layer.</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;">Layer geometry</span></p></p><p><p><span style="font-size: 16px;">The frame/bounds/center of the UIView corresponds to Calayer frame/bounds/position,center and position are the anchorpoint of the parent layer, UIView frame/ Bounds/center is just the access method, manipulating the UIView of these properties is actually changing the calayer corresponding to these properties. the frame property of Calayer is also a computed property, which is based on the bounds/position/ Transform three attributes are calculated, and you change the value of frame can also affect the value of it, if you do rotation and zoom frame and bounds may no longer be consistent, bounds is wide, and the frame also calculates the rotation of the X and Y axes occupied space, such as</span></p></p><p><p><span style="font-size: 16px;">Calayer control the position of UIView by Anchorpoint (anchor Point) and center (position), The anchor point is a relative UIView position, and center is a point, Because the Anchorpoint property is masked for uiview, and the Anchorpoint default is {0.5,0.5}, So this property is called Center. and the UIView and calayertransform revolve around this anchorpoint, and if it's a circular motion (like a clock rotation), you need to set the value of the anchor point so that it rotates normally.</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Both Calayer and UIView have a set of positions that can convert it relative to the current parent layer (or view), Mac OS and iOS coordinate systems are the opposite, iOS left upper left Mac oc, You can use the Geometryflipped property of the layer to fit it, which flips the coordinate system. the Zposition property of the layer is set to the position of its vertical axis, which is 0 by default, so if you set it to 1, it will appear on top of the other Layers.</span></p></p><p><p><span style="font-size: 16px;">you can determine whether the clicked point is on a layer or view by relative coordinate transformation</span> , and the Code:</p></p><pre><span style="font-size: 14px;"><span style="color: #008080;"><span style="color: #008080;">1</span></span>var point = (touches<span style="color: #0000ff;"><span style="color: #0000ff;"></span> as</span>nsset). Anyobject ()?<span style="color: #000000;"><span style="color: #000000;">. Locationinview (self.view)</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Point = Bluelayer.convertpoint (point!<span style="color: #000000;"><span style="color: #000000;">, FromLayer:self.view.layer)</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Bluelayer.containspoint (point!<span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Touch in Blue</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span>Let Yellowpoint = Yellowlayer.convertpoint (point!<span style="color: #000000;"><span style="color: #000000;">, Fromlayer:bluelayer)</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">yellowlayer.containspoint (yellowpoint) {</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Touch in Yellow</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span>Let Redpoint = Redlayer.convertpoint (point!<span style="color: #000000;"><span style="color: #000000;">, Fromlayer:bluelayer)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">redlayer.containspoint (redpoint) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Touch in Red</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</span></pre><p><p></p></p><p><p> <span style="font-size: 16px;">HitTest can get the layer you're in touch with:</span></p></p><pre><pre><span style="font-size: 14px;">Let point = (touches as nsset). anyobject ()?. Locationinview (self.view) Let layer = Self.view.layer.hitTest (point!) if layer = = Bluelayer { println ("touch in blue")}if layer = = Yellowlayer { println ("touch in yellow")}if layer = = Redlayer { println ("touch in red")}</span></pre></pre><p><p> <span style="font-size: 16px;">UIView can be autoresizingmask and constraints and other properties to achieve adaptive screen rotation, Calayer also have corresponding LayoutManager properties and Caconstraintlayoutmanager class, But only on Mac os, not supported on ios, you can't use layer alone if you want to use this feature, but if you want to resize the layer or you can set the Layer's delegate, Then implement the Proxy method Layoutsublayersoflayer directly modify the size of the color, it also needs to call the Setneedslayout method, which is the same as UIView corresponding layoutsubviews.</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;">Visual effects</span><br><span style="font-size: 16px; color: #339966;">Rounded Corners</span><br><span style="font-size: 16px;">The Cornerradius property of the layer can set the fillet curvature, if the curvature size is half the length of the edge, the fillet will be tangent to this edge, if it is a square the final result is a circle, if it is clipped sub-view is directly within the circle crop</span></p></p><p><p><span style="font-size: 16px; color: #339966;">Border</span><br><span style="font-size: 16px;">Layer BorderColor Set the border color, it is cgcolorref,borderwidth set the border width, it is worth noting that the border width is occupied by the width of the frame of the layer, It does not add a border outside the layer but is internally generated. the layer is covered by a layer, but the border is not overwritten, and the border is only displayed, it does not interfere with touch and events, and there are no borders that are the Same.</span></p></p><p><p><span style="font-size: 16px; color: #339966;">Shadow</span><br><span style="font-size: 16px;">The shadow requires at least shadowcolor/shadowoffset/shadowopacity three properties to work, the Shadowoffset value is the Cgsize type, and two positive values are in the Lower-right corner. there is also a property shadowradius, It controls the bounds of the shadow blur degree, the default value is 3, the value of 0 is not blurred, the higher the value is blurred, the result of the blur is Cgsize assured color weight, The other several directions also have a corresponding shadow, there will be a sense of hierarchy.</span><br><span style="font-size: 16px;">Because the shadow is outside the layer, if you want to crop a sub-view that is outside the layer, you need to use the Masktobounds property, and the shadow is clipped out, so you need special handling when masktobounds and shadows coexist:</span></p></p><pre><span style="font-size: 14px;"><span style="color: #008080;"><span style="color: #008080;">1</span></span>Bluelayer =<span style="color: #000000;"><span style="color: #000000;">Calayer ()</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Bluelayer.frame = CGRectMake (<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Bluelayer.backgroundcolor =<span style="color: #000000;"><span style="color: #000000;">Uicolor.bluecolor (). Cgcolor</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Bluelayer.cornerradius = blueLayer.bounds.size.width/<span style="color: #800080;"><span style="color: #800080;">2.0</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>Bluelayer.bordercolor =<span style="color: #000000;"><span style="color: #000000;">Uicolor.purplecolor (). Cgcolor</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>Bluelayer.borderwidth =<span style="color: #800080;"><span style="color: #800080;">10.0</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span>Bluelayer.maskstobounds =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Let Shadowlayer =<span style="color: #000000;"><span style="color: #000000;">Calayer ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>Shadowlayer.frame =<span style="color: #000000;"><span style="color: #000000;">Bluelayer.frame</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>Shadowlayer.cornerradius =<span style="color: #000000;"><span style="color: #000000;">Bluelayer.cornerradius</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Shadowlayer.shadowcolor =<span style="color: #000000;"><span style="color: #000000;">Uicolor.redcolor (). Cgcolor</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Shadowlayer.shadowoffset = Cgsizemake (<span style="color: #800080;"><span style="color: #800080;"></span> at</span>,<span style="color: #800080;"><span style="color: #800080;">23.0</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Shadowlayer.shadowopacity =<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Shadowlayer.shadowradius =<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Shadowlayer.backgroundcolor =<span style="color: #000000;"><span style="color: #000000;">Uicolor.redcolor (). Cgcolor</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Self.view.layer.addSublayer (shadowlayer)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>Self.view.layer.addSublayer (bluelayer)</span></pre><p><p><span style="font-size: 16px;">It is important to note that the shadow layer needs a background color, otherwise its shadow is not displayed.</span></p></p><p><p><span style="font-size: 16px;">The Shadowpath property can set the shape of the shadow, note that cgmutablepath in Swift does not increase the reference count, you do not need to relase it and there is no way to give you release</span></p></p><pre><pre><span style="font-size: 14px;"><span style="color: #008080;">1</span> Let circlepath =<span style="color: #000000;"> cgpathcreatemutable ()</span><span style="color: #008080;">2</span> <span style="color: #000000;"> cgpathaddellipseinrect (circlepath, nil, Self.blueLayer.bounds)</span><span style="color: #008080;">3</span> Shadowlayer.shadowpath = Circlepath</span></pre></pre><p><p><span style="font-size: 16px;">Of course you can also use Uibezierpath to set more complex shapes for Shadowpath</span></p></p><pre><span style="font-size: 14px;"><span style="font-size:14px;"><span style="color: #008080;">1 </span> Let path1 = Uibezierpath (roundedrect: CGRectMake (-<span style="color: #800080;">75 </span>,-<span style="color: #800080; >75 </span>, <span style=" color : #800080; ">200 </span>, <span style="color: #800080;">200 </span>), cornerradius: <span style="color: #800080;">30 </span> Span style= "color: #000000;" >). Cgpath </span> <span style="color: #008080;">2 </span> Let path2 = Uibezierpath (arccenter:cgpointmake (<span style=" Color: #800080; ">25 </span>, <span style="color: #800080;">40 </span>), radius: <span style="color: #800080;">100 </span>, startangle : <span style="color: #800080;">0 </span>, endangle: <span style="color: #800080;">3.14159265 </span>, clockwise: Span style= "color: #0000ff;" >true <span style="color: #000000;">). Cgpath </span> <span style="color: #008080;">3 </span> shadowlayer.shadowpath =<span style="color: #000000;"> path1 </span> <span style="color: #008080;">4 </span> shadowlayer.shadowpath = path2 </span></pre><p><p> <span style="font-size: 16px;"> <span style="color: #ff0000;">Picture Mask</span></span></p></p><p><p><span style="font-size: 16px;"><em id="__mceDel">The Mask property of the layer can be set to a layer to him, the layer of the contents should be a 32-bit with an alpha channel PNG picture, You can set an irregular picture of the other parts as transparent, so that the layer set a mask, The color of the mask is unimportant, the contour is more important, and finally the mask layer is set it will only show the contents of the Mask mask shape.</em></span></p></p><pre><pre><span style="font-size: 14px;"><span style="color: #008080;">1</span> Let Masklayer =<span style="color: #000000;"> calayer ()</span><span style="color: #008080;">2</span> masklayer.frame = CGRectMake (<span style="color: #800080;">0</span><span style="color: #800080;">0</span><span style="color: #800080;"></span> <span style="color: #800080;"></span> - <span style="color: #000000;">)</span><span style="color: #008080;">3</span><span style="color: #800000;">"</span><span style="color: #800000;">111.png</span><span style="color: #800000;">"</span>)? <span style="color: #000000;">. Cgimage</span><span style="color: #008080;">4</span> bluelayer.mask = Masklayer</span></pre></pre><p><p></p></p><p><p><span style="font-size: 16px; color: #ff0000;"><em>Stretch</em></span></p></p><p><p><em>If you set a picture does not need to stretch a lot of benefits, that is, do not need to stretch the picture, but also reasonable use of memory and cpu, but many times a picture to be used in multiple locations, so need to stretch, iOS and we provide 3 of the stretching method</em> <span style="line-height: 1.5;">kcafilterlinear/</span> <span style="line-height: 1.5;">kcafilternearest/</span> <span style="line-height: 1.5;">kcafiltertrilinear, You can set the stretching method only with the two properties of the layer to assign the corresponding stretching method: minification (zoom out) and magnification (enlarge picture), This property by default is kcafilterlinear, it is similar to kcafiltertrilinear, they are linear, meaning that it will take two values of the excessive color, so that the corresponding point can be smooth transition, if the picture has a gradient and diagonal more than the need to use this default, If the picture is mostly monochrome and is predominantly vertically oriented, then it needs to be kcafilternearest, it is violent to take the surrounding color directly, so it will not be distorted, and will not consume too much CPU</span></p></p><p><p><span style="line-height: 1.5;">  <span style="color: #ff0000; font-size: 16px;">Group Transparency</span></span></p></p><p><p><span style="line-height: 1.5;">IOS8 default is group transparency, before applying transparency, it will be the layer and it integrated into a whole picture, so there is no transparency mixing problem, There is no test of transparency problems, if there is a layer can be set shouldrasterize value is yes</span></p></p><p><p></p></p><p><p>The Calayer of advanced techniques in iOS core animation (i)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.