iOS Development UI Chapter-calayer (Create Layer) (go to)

Source: Internet
Author: User

iOS Development UI Chapter-calayer (custom layer)

One, the first way

1. Brief description

Before you want to draw something in view, you need to customize the view, create a class associated with it, let the class inherit from UIView, and then rewrite its drawrect: method, and then draw in the method.

To draw a drawing: (1) Get context (2) draw a graphic (3) render a graphic similar to the previous procedure if you draw something on a layer. code example: Create a new class to inherit from the Calayeryymylayer.m file
1//2//  YYMYLAYER.M 3//  05-Custom Layer (1) 4//5//  Created by Apple on 14-6-21.6/  Copyright (c) 2014 Itcase. All rights reserved. 7//8  9 #import "YYMylayer.h" @implementation YYMylayer12//Override this method to draw a graphic within the method (void) Drawincontext: ( Cgcontextref) ctx14 {     //1. Draw a shape from     //Draw a circle     of Cgcontextaddellipseinrect (CTX, CGRectMake (50, 50, 100, 100)) /     //Set properties (color)    : [[Uicolor yellowcolor]set];20     cgcontextsetrgbfillcolor (ctx, 0, 0, 1, 1);     22     //2. Rendering     Cgcontextfillpath (CTX);}25 @end
In the controller, create a custom class
1//2//  YYVIEWCONTROLLER.M 3//  05-Custom Layer (1) 4//5//  Created by Apple on 14-6-21.6/  Copyright (c) 2014 itcase. All rights reserved. 7//8  9 #import "YYViewController.h" #import "YYMylayer.h" @interface Yyviewcontroller () @end15 @impl Ementation YYViewController17-(void) viewDidLoad19 {     [super viewdidload];21]     //1. Create a custom Layer23     yymylayer *layer=[yymylayer layer];24     //2. Set properties for layer (     Layer.backgroundcolor=[uicolor Browncolor]. cgcolor;26     layer.bounds=cgrectmake (0, 0, $);     layer.anchorpoint=cgpointzero;28     Layer.position=cgpointmake (+),     layer.cornerradius=20;30     layer.shadowcolor=[uicolor blackColor ]. Cgcolor;31     Layer.shadowoffset=cgsizemake (Ten),     layer.shadowopacity=0.6;33     [layer Setneedsdisplay];35     //3. Add layer36     [Self.view.layer addsublayer:layer];37-     }39 @end
Note: (1) The default is colorless and will not be displayed. To make the drawing appear, you also need to set the color of the graphic. Note You cannot use the class in the UI framework directly (2) in a custom layer-(void) Drawincontext: The method does not call itself, only by itself through the Setneeddisplay method call, in the view to draw things drawrect: method is called automatically when the view is first displayed. Achieve the effect: 2. Expand the drawing instructions in UIView
1 #import "YYVIEW.h" 2  3 @implementation yyview 4  5  6-(void) DrawRect: (cgrect) rect 7 {8     //1. Get Context 9     Cgcontextref Ctx=uigraphicsgetcurrentcontext ();     //2. Draw the graph one     by one cgcontextaddellipseinrect (CTX, CGRectMake ( (+), +/     /Set properties (color): [    [Uicolor yellowcolor]set];14     Cgcontextsetrgbfillcolor (ctx , 0, 0, 1, 1);     //3. Render     Cgcontextfillpath (CTX);     when performing rendering operations, it is inherently equivalent to invoking the following method     [Self.layer drawincontext:ctx];20}

Description: Draws a graphic in UIView, and gets the context of the view corresponding to the layer. When rendering, the graphic is rendered to the corresponding layer.

When performing rendering operations, it is inherently equivalent to executing [self.layer drawincontext:ctx];

Ii. Second Way

Method Description: Set Calayer delegate, and then let delegate implement Drawlayer:incontext: Method, Calayer delegate is called when Drawlayer:incontext requires drawing: method to draw.

code example:

 1//2//YYVIEWCONTROLLER.M 3//06-Custom Layer (2) 4//5//Created by Apple on 14-6-21. 6//Copyright (c) 2014 itcase. All rights reserved. 7 8 #import "YYViewController.h" 9 @interface yyviewcontroller () @end11 @implementation YYViewController13-(VO ID) viewDidLoad15 {[Super viewdidload];17//1. Create a custom layer18 calayer *layer=[calayer layer];19//2. Setting Laye Properties of R Layer.backgroundcolor=[uicolor Browncolor]. Cgcolor;21 layer.bounds=cgrectmake (0, 0, $); layer.anchorpoint=cgpointzero;23 Layer.position=cgpointm Ake (+), layer.cornerradius=20;25 Layer.shadowcolor=[uicolor Blackcolor]. Cgcolor;26 Layer.shadowoffset=cgsizemake (Ten), layer.shadowopacity=0.6;28 29//Set agent at Layer.deleg ate=self;31 [Layer setneedsdisplay];32//3. Add layer33 [Self.view.layer addsublayer:layer];34}35-(void) Draw Layer: (Calayer *) layer Incontext: (cgcontextref) ctx37 {38//1. Drawing 39//Draw a circle and CgcontextadDellipseinrect (CTX, CGRectMake (50, 50, 100, 100)); 41//Set properties (color)/[[Uicolor yellowcolor]set];43 Cgcontex Tsetrgbfillcolor (CTX, 0, 0, 1, 1); 44 45//2. Render Cgcontextfillpath (CTX);}48 @end

Implementation results:

Note: You can no longer set a uiview to Calayer delegate because the UIView object is already the delegate of its internal root layer, and the delegate that is set to another layer will be problematic.

When setting up an agent, it does not require us to comply with the agreement, stating that this method is NSObject, there is no need for additional display compliance protocol. Tip: If you want to set a proxy for a class in the future, but this agent does not require us to comply with any specific protocol, then we can think of this protocol method is nsobject inside. Iii. Additional Information(1) Regardless of which method you take to customize the layer, you must call Calayer's Setneedsdisplay method to draw properly. (2) Detailed realistic process: when UIView needs to be displayed, its inner layer prepares a cgcontextref (graphics context) and then calls delegate (here is UIView) Drawlayer:incontext: Method, And pass in the Cgcontextref object that is already ready. and UIView in the Drawlayer:incontext: method will also call their own DrawRect: method. Usually in DrawRect: through Uigraphicsgetcurrentcontext () is the Cgcontextref object that is passed in by the layer, all the drawings completed in DrawRect: are filled into the cgcontextref of the layer, It is then copied to the screen.

iOS Development UI Chapter-calayer (Create Layer) (go to)

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.