calayer-custom layer of iOS core animation

Source: Internet
Author: User
This article directory one, the custom layer method 12, the custom layer method 23, other

The custom layer, in fact, is the drawing on the layer, a total of 2 methods, described in detail below. Back to the top one, custom layer Method 1

Method Description: Create a Calayer subclass, and then overwrite the Drawincontext: method, using the Quartz2d API for drawing 1. Create a subclass of Calayer

2. Overwrite drawincontext in. m file: Method, drawing inside

1 @implementation mjlayer
 2 
 3 #pragma mark draws a solid triangle
 4-(void) Drawincontext: (cgcontextref) CTX {
 5     //Set to Blue
 6     cgcontextsetrgbfillcolor (ctx, 0, 0, 1, 1);
 7 
 8     
 9     //Set beginning     cgcontextmovetopoint (CTX, 0);
One     ///from (50, 0) wired to (0,
cgcontextaddlinetopoint) (     ctx, 0,);
(     0, 100) wired to (Cgcontextaddlinetopoint) (CTX, MB     );     the merge path, the connection starting point and the endpoint     Cgcontextclosepath (CTX);     //Draw path     Cgcontextfillpath (CTX); 
@end

3. Add layers to the screen on the controller

1 Mjlayer *layer = [Mjlayer layer];
2//Set layer width of
3 layer.bounds = CGRectMake (0, 0, M, m);
4//Set the position of the layer
5 layer.position = cgpointmake (MB);
6//Start drawing layer
7 [layer Setneedsdisplay];
8 [Self.view.layer Addsublayer:layer];

Note that line 7th calls the Setneedsdisplay method before triggering the Drawincontext: the call to the method, and then drawing

Back to top two, custom layer Method 2

Method Description: Sets the delegate of the Calayer and then lets delegate implement Drawlayer:incontext: method, when Calayer needs a drawing, the delegate Drawlayer:incontext is invoked: method to draw.

* Note here: You can no longer set a UIView to Calayer's delegate, because the UIView object is already the delegate of its internal root layer, and the delegate that is set to another layer is problematic. UIView and its internal calayer default diagram:

1. Create a new layer, set delegate, and add it to the layer of the controller's view

1 Calayer *layer = [Calayer layer];
 2//Set delegate
 3 layer.delegate = self;
 4//Set layer width of
 5 layer.bounds = cgrectmake (0, 0, M, m);
 6//Set the position of the layer
 7 layer.position = cgpointmake (MB);
 8//Start drawing layer
 9 [layer Setneedsdisplay];
[Self.view.layer Addsublayer:layer];

* Set the Calayer delegate in line 3rd, where self refers to the controller

* Note the 9th line, you need to call Setneedsdisplay this method, will notify delegate to draw

2. Let the Calayer delegate (the controller set up above) realize Drawlayer:incontext: Method

1 #pragma mark draws a rectangular box
 2-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX {
 3     //Set Blue
 4     cgcontextsetrgbstrokecolor (ctx, 0, 0, 1, 1);
 5     //Set Border width
 6     cgcontextsetlinewidth (CTX);
 7     
 8     //Add a rectangle with the same size as the layer to the path of
 9     cgcontextaddrect (CTX, layer.bounds);
Ten     
11     
Related Article

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.