Ios:calayer drawing on the core animation layer

Source: Internet
Author: User

To draw on Calayer:

• To draw on Calayer, there are two methods:1. Create a Calayer subclass, and then overwrite the Drawincontext: method, where you can draw using the Quartz2d API2. Set the Calayer delegate and let delegate implement the Drawlayer:incontext: method to draw• Note:– The UIView can no longer be set to this Calayer delegate because the UIView object is already a delegate of the inner layer, and setting again will cause problems– Regardless of which method you use, you must send a setneedsdisplay message to the layer to trigger a call to the appropriate drawing method relationships between Calayer, UIView, and context:• When UIView receives the Setneedsdisplay message, Calayer prepares a cgcontextref, sends a message to its delegate, UIView, and passes in the prepared Cgcontextref object. UIView in the Drawlayer:incontext: method calls its own DrawRect: Method• Usually in DrawRect: Uigraphicsgetcurrentcontext () is obtained by Calayer incoming Cgcontextref object, in DrawRect: All drawings completed in the Calayer are filled into the cgcontextref, and then copied to the screenCalayer cgcontextref with bitmap contexts (Bitmap Graphics context)let's take a concrete example of both of these plotting methods: mode one : by setting Calayer Proxy and Implementing proxy method Drawlayer:incontext: The form of the drawing//In-(void) viewdidload{[super viewdidload]; The code is:To create a child layer:
    // Create a child layer (using the default anchor point)    Calayer *sublayer = [[Calayer alloc]init];         = CGRectMake (0,0,+);         = Self.view.center;         = [[Uicolor Redcolor]cgcolor];    
//Set fillet radius, set to 50, at which point the sub-layer is circular - ; [Self.view.layer Addsublayer:sublayer];
Set up proxy
    // to set up a proxy    for a layer Sublayer. delegate = self;
Send Redraw message
    // the proxy method is only invoked when the message is sent Setneedsdisplay    [Sublayer Setneedsdisplay];
//Implement proxy Drawlayer:incontext: The method code is:
proxy methods for #pragma mark-calayer-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) ctx{//Draw a rectangleCgcontextaddrect (CTX, CGRectMake (0,0, -, -));
//Set color CGFloat components[4] = {0.0,1.0,0.0,1.0}; Cgcontextsetstrokecolor (CTX, components);

This way you can also set the color//cgcontextsetstrokecolorwithcolor (Ctx,[[uicolor greencolor]cgcolor]);
//Rotate coordinate systemCGCONTEXTSCALECTM (CTX,1, -1); CGCONTEXTTRANSLATECTM (CTX,0, - -); //draw a smiley face imageUIImage *image = [UIImage imagenamed:@"1.png"]; Cgcontextdrawimage (CTX, CGRectMake (0,0, -, -), [Image Cgimage]); //Draw PathCgcontextdrawpath (CTX, Kcgpathstroke);}
The results are as follows: A smiley face image is drawn in the child layer, and a green-edged rectangle is drawn in the upper-left corner . Method Two : Draw by creating a subclass of Calayer and overriding the Drawincontext: Method//First in the Controller class-(void) viewdidload{[super viewdidload]; The code for creating a child layer in a method is:
#import "ViewController.h"#import "MyLayer.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Create a child layerMylayer *mylayer =[[Mylayer alloc]init]; Mylayer.bounds= CGRectMake (0,0, -, -); Mylayer.position= Cgpointmake ( -, -);
//Set sub-layer background color to gray Mylayer.backgroundcolor=[[Uicolor Graycolor]cgcolor]; [Self.view.layer Addsublayer:mylayer]; [Mylayer Setneedsdisplay];}@end
//Then create a subclass of Calayer, flush the Drawincontext in the class: method, code as follows:
#import "MyLayer.h"#import<UIKit/UIKit.h>@implementationMylayer//Override this method-(void) Drawincontext: (cgcontextref) ctx{//Draw a rectangleCgcontextaddrect (CTX, CGRectMake (0,0, -, -)); //The first way to set the color://Set color space (choose color Scheme: RGB, red, green, blue)Cgcolorspaceref ColorSpace =Cgcolorspacecreatedevicergb (); Cgcontextsetstrokecolorspace (CTX, colorspace);
///array of four contents: the first three are red, green, blue, and the latter is the transparency CGFloat components[4] = {0.0,1.0,0.0,1.0}; Cgcontextsetstrokecolor (CTX, components); //This is another simple way to set the color//Cgcontextsetstrokecolorwithcolor (CTX, [[Uicolor Greencolor]cgcolor]); //Draw a stroke pathCgcontextdrawpath (CTX, Kcgpathstroke); //release the Create-out attribute to prevent memory leakscgcolorspacerelease (colorspace);}@end
The results are as follows: A gray layer of layers is created, and a rectangle with a green edge is drawn on it.

Ios:calayer drawing on the core animation layer

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.