The same page multiple Calayer redraw method

Source: Internet
Author: User

//Knowledge point, Calayer Redraw,-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTx method, Calayer gradient. Multiple Calayer redraw methods.

This example is a, Viewcontroller class that does not inherit any delegate,

That is, the bottom CA1,CA2,CA3 delegate is set directly to self without inheriting a delegate like <UITextFieldDelegate>. Calyer seems to have no commission, just use it. Don't make it clear, just write it straight.

Test this example with a random build based on the Uiviewcontroller class, I chose Singleviewapplication.

( standalone view application) the fourth of the first row, just a name, go in and change all the Animationcontroller(my project name) to the name of your own project. You can run it.

//You have to add a Quartzcore.framework, How to add not to say, has said very clearly, where will not leave a message ...

//How to add Quartzcore.framework still say it:

1. Click on the item on the left

2, the middle position to drag a little, see four Yellow small toolbox, the lower left corner has a plus, click out a lot of yellow small box, find quartzcore.framework point Add. It is added and then declared in the M file, which is a necessary process, like the #import <QuartzCore/QuartzCore.h> in my m file.



H file:

#import <UIKit/UIKit.h>

@interface Animationcontroller:uiviewcontroller

Notice there's no < Commission behind Uiviewcontroller >;

@property (nonatomic, retain) Calayer *layer1;

@property (nonatomic, retain) Calayer *layer2;

@property (nonatomic, retain) Calayer *layer3;

Definition of three calayer *layer1,*layer2,*layer3;

@end

M File:

#import "AnimationController.h"

#import <QuartzCore/QuartzCore.h>

@interface Animationcontroller ()

#define UICOLORFROMRGB (rgbvalue) [Uicolor colorwithred: ((float) ((Rgbvalue & 0xFF0000) >>16)/255.0 Green: (( float) ((Rgbvalue & 0xff00) >> 8)/255.0 Blue: ((float) (Rgbvalue &0xff))/255.0 alpha:1.0]

This place is a constant, used to set the RGB color, do not have to write, my example used, so please copy, pretty useful, you can still use later.

@end

@implementation Animationcontroller

@synthesize layer1 = _layer1;

@synthesize layer2 = _layer2;

@synthesize Layer3 = _layer3;

//Top is 3 Calayer class _layer1,_layer2,_layer3, which is the *layer1 in H file ; *layer2; *layer3, this is the iOS development mechanism, does not explain;

-(void) viewdidload

{

[Super Viewdidload];

Self.view.layer.backgroundColor = [Uicolor Whitecolor]. Cgcolor;

Sets the self.view.layer background color.

Calayer *CA1 = [Calayer layer];

Instantiate a CALAYER:CA1;

Ca1.backgroundcolor = [Uicolor Whitecolor]. Cgcolor;

Sets the background color of the CA1.

Ca1.frame = CGRectMake (50, 500, 200, 200);

Sets the frame (coordinates and size) of the CA1;

Ca1.delegate = self;

Ca1.delegate = self; This step is very important,

Ca1.name = @ "Fuck";

This ca1.name setting is not set to all lines, not used.

[CA1 Setneedsdisplay];

CA1 calls Setneedsdisplay to draw its own page.

Calayer *CA2 = [Calayer layer];

Ca2.backgroundcolor = [Uicolor Orangecolor]. Cgcolor;

Ca2.frame = CGRectMake (300, 500, 200, 200);

Ca2.name = @ "You";

Ca2.delegate = self;

[Ca2 Setneedsdisplay];

There is no difference between Ca2 and CA1, that is, the color and coordinates are different, and the name is different.

Calayer *CA3 = [Calayer layer];

Ca3.backgroundcolor = [Uicolor Yellowcolor]. Cgcolor;

Ca3.frame = CGRectMake (550, 500, 200, 200);

Ca3.name = @ "man";

Ca3.delegate = self;

[CA3 Setneedsdisplay];

There is no difference between CA3 and CA1, that is, the color and coordinates are different, and the name is different.

[Self.view.layer ADDSUBLAYER:CA1];

Add CA1 to Self.view.layer, and remember to add it to the Self.view layer.

[Self.view.layer ADDSUBLAYER:CA2];

Add Ca2 to Self.view.layer

[Self.view.layer ADDSUBLAYER:CA3];

Add CA3 to Self.view.layer

Self.layer1 = CA1;

//Assign CA1 to Self.layer1;

Self.layer2 = CA2;

assigning Ca2 to Self.layer2;

Self.layer3 = CA3;

assigning Ca2 to Self.layer3;

}

This method is important:-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX, passing in two parameters (Calayer *) of type layer and ( CGCONTEXTREF) type CTX, which means that we call [CA1 Setneedsdisplay] when he passes in LAYER==CA1 because we assign the CA1 to self. Layer1 So layer also ==SELF.LAYER1,CTX is CA1 this layer of context, also is equivalent to an artboard, let us redraw on the top, this method is equivalent to UIView:-(void)drawrect; , understood, understood, understood, and you knew he was coming in. Two parameters, a layer, an artboard, when we call [CA1 Setneedsdisplay];

-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX

{

NSLog (@ "Layer.name = =%@", layer.name);

Output the name of the current layer, optional, just tell us which layer is currently.

if (layer = = Self.layer1)

Here to add a judge, when the layer is Self.layer1 is a redraw method, that is, three layers of self.layer1,self.layer2, Self.layer3 You may need a painting tree, a painting grass, a picture of a bird, just for example oh, I can't draw a bird. I am painting here, Self.layer1: A gradient layer, the upper right corner of a gray triangle symbol, SELF.LAYER2: in the upper right corner of a layer painted a blue triangle symbol, SELF.LAYER3: in the upper right corner of a layer painted a black triangle symbol.

{

Cggradientref mygradient;

Cgcolorspaceref Mycolorspace;

size_t locationcount = 3;

CGFloat locationlist[] = {0.0,0.1,1.0};

CGFloat colorlist[]={

1.0,0.0,0.5,1.0,

1.0,0.0,1.0,1.0,

0.3,0.5,1.0,1.0,

};

Mycolorspace = Cgcolorspacecreatedevicergb ();

Mygradient = Cggradientcreatewithcolorcomponents (Mycolorspace, ColorList, Locationlist, locationCount);//The core function is this, To figure out some quantitative stuff.

Cgpoint Startpoint,endpoint;

startpoint.x = 0;

Startpoint.y = 0;

Endpoint.x = Cgrectgetmaxx (layer.bounds);

Endpoint.y = Cgrectgetmaxy (layer.bounds);

Cgcontextdrawlineargradient (CTX, Mygradient, StartPoint, EndPoint, 0);//This is drawn, and you can trim to finish a particular shape.

Cgcolorspacerelease (Mycolorspace);

Cggradientrelease (mygradient);

Cgcontextsetlinecap (CTX, Kcglinecapsquare);

Cgcontextsetlinewidth (CTX, 1.0);

Cgcontextsetrgbstrokecolor (CTX, 1.0, 0.0, 0.0, 1.0);

//Top is the process of setting the gradient of the layer, the code is specific I'm not going to talk about it. Some of the drawing code for iOS, which is a split line, is a simple triangle of the picture below.        

Cgcontextbeginpath (CTX);

//Start a path

Cgcontextmovetopoint (CTX, Layer.bounds.size.width * 3/4,LAYER.BOUNDS.ORIGIN.Y + 1);

Set a starting point coordinate

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, Layer.bounds.size.width *1/4);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, Layer.bounds.size.width * 3/4, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextsetfillcolorwithcolor (CTX, Uicolorfromrgb (0x555555). Cgcolor);

Set the fill color, here is used in our definition of the UICOLORFROMRGB () is usually entered in this parenthesis is (0x******) 0 x After the beginning of the 6-bit 16 RGB color number, 0x unchanged transformation behind six digits can change the color, online a search a lot, or my blog to find a dedicated RGB color site collection.

Cgcontextfillpath (CTX);

Fill.

Cgcontextstrokepath (CTX);

Connecting all the points, it becomes a triangle.

}

if (layer = = Self.layer2)

{

Cgcontextsetlinecap (CTX, Kcglinecapsquare);

Cgcontextsetlinewidth (CTX, 1.0);

Cgcontextsetrgbstrokecolor (CTX, 1.0, 0.0, 0.0, 1.0);

Cgcontextbeginpath (CTX);

Cgcontextmovetopoint (CTX, Layer.bounds.size.width * 3/4,LAYER.BOUNDS.ORIGIN.Y + 1);

Set a starting point coordinate

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, Layer.bounds.size.width *1/4);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, Layer.bounds.size.width * 3/4, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextsetfillcolorwithcolor (CTX, Uicolorfromrgb (0X8F9FEA). Cgcolor);

Cgcontextfillpath (CTX);

Cgcontextstrokepath (CTX);

This is the same as the Self.layer1 triangle, but I changed his RGB to make it easy to distinguish.

To connect these coordinates.

}

if (layer = = Self.layer3)

{

Cgcontextsetlinecap (CTX, Kcglinecapsquare);

Cgcontextsetlinewidth (CTX, 1.0);

Cgcontextsetrgbstrokecolor (CTX, 1.0, 0.0, 0.0, 1.0);

Cgcontextbeginpath (CTX);

Cgcontextmovetopoint (CTX, Layer.bounds.size.width * 3/4,LAYER.BOUNDS.ORIGIN.Y + 1);

Set a starting point coordinate

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, layer.bounds.size.width-1, Layer.bounds.size.width *1/4);

Move to the coordinates of the next point

Cgcontextaddlinetopoint (CTX, Layer.bounds.size.width * 3/4, LAYER.BOUNDS.ORIGIN.Y + 1);

Move to the coordinates of the next point

Cgcontextsetfillcolorwithcolor (CTX, Uicolorfromrgb (0x000000). Cgcolor);

Cgcontextfillpath (CTX);

Cgcontextstrokepath (CTX);

This is the same as the Self.layer2 triangle, it just changes the color.

}

}

@end The result of the operation is this:For a long time, want to facilitate you, take away please indicate the source.

The same page multiple Calayer redraw method

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.