iOS development-calayer and UIView detailed summary

Source: Internet
Author: User

1. The relationship between Calayer and UIView:

    • In iOS, the things you can see are basically uiview, such as UI controls, icons, and so on, all UIView.
    • In fact, UIView can be displayed on the screen entirely because of its inner layer (calayer).
    • When you create a UIView object, a layer (that is, the Calayer object) is automatically created inside UIView, which can be accessed through the Layer property of the UIView. When the UIView needs to be displayed on the screen, the DrawRect: method is called to draw, and all the content is drawn on its own layer, and after the drawing is finished, the layer is copied to the screen, so the UIView is displayed.
    • In other words, the UIView itself does not have the ability to display, it is the inner layer of the display function.
    • UIView can be displayed entirely because of the internal Calayer object. Therefore, by manipulating this Calayer object, you can easily adjust some of the interface properties of the UIView, such as: Shadow, fillet size, border width, color, and so on.

To set the fillet and rotation of a picture:

1Uiimageview *im =[[Uiimageview alloc]init];2Im.frame = CGRectMake ( -, -, -, -);3Im.image = [UIImage imagenamed:@"Collection Background"];4Im.layer.cornerRadius =Ten;5Im.layer.masksToBounds =YES;6Im.layer.transform = Catransform3dmakerotation (M_pi_4,0,0,1);7[Self.view Addsubview:im];

2.UIView can add a child view by Addsubview: method, similarly, Calayer can add a child layer by Addsublayer: method

1Calayer *mylayer =[Calayer layer];2  //set the width and height of the layer (100x100)3Mylayer.bounds = CGRectMake (0,0, -, -);4   //set the location of the layer5Mylayer.position = Cgpointmake ( $, -);6   //set up a picture to display7Mylayer.contents = (ID) [UIImage imagenamed:@"0000.png"]. cgimage;8   //sets the fillet radius of the layer to ten9Mylayer.cornerradius =Ten;Ten  //If you set the picture, you need to set this property to Yes to have rounded effect OneMylayer.maskstobounds =YES; A   -  //add Mylayer to the layer of the controller's view -[Self.view.layer Addsublayer:mylayer];
Supplement: Cgimage is a cgimageref type of data
For example, when dealing with a picture only shows a part of the time, the use of cgimageref
1UIImage *ima = [UIImage imagenamed:@"Collection Background"];2 //the part of the picture that needs to be displayed3CGRect rect = CGRectMake ( -, -, -, -);4 //get the data for the display section5Cgimageref Imageref =cgimagecreatewithimageinrect ([Ima cgimage], rect);6 //turn data into a picture7UIImage *subimage =[UIImage imagewithcgimage:imageref];8Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (0,0, -, -)];9Imageview.image =Subimage;Ten[Self.view Addsubview:imageview];

3. Why do I use Cgcolorref and Cgimageref in Calayer for 2 types of data without Uicolor and uiimage?

* First of all to know: Calayer is defined in the Quartzcore framework, CGIMAGEREF, cgcolorref two data types are defined in the Coregraphics framework; Uicolor, UIImage is defined in the Uikit frame

* Second, the Quartzcore framework and the Coregraphics framework are available across platforms and can be used on both iOS and Mac OS x, but Uikit can only be used in iOS

* Therefore, in order to ensure portability,Quartzcore can not use UIImage, Uicolor, can only use Cgimageref, Cgcolorref

* However, in many cases, the Coregraphics object can be obtained by Uikit the specific method of the object, such as UIImage Cgimage method can return a cgimageref

4. How do I choose UIView and Calayer?

UIView is more an event-handling function than Calayer, and Calayer is unable to handle user touch events. So if the displayed things need to interact with the user, with UIView, if you do not need to interact with the user, with UIView or calayer can be.

5. In fact, the UIView display process

When UIView needs to be displayed, its inner layer prepares a cgcontextref (graphics context) and calls the Drawlayer:incontext of delegate (here is UIView): method, And pass in the Cgcontextref object that is already ready. and UIView in Drawlayer:incontext: The method calls its own DrawRect: method

In DrawRect: Uigraphicsgetcurrentcontext () Gets the Cgcontextref object that is passed in from the layer, and all the drawings completed in DrawRect: are filled in the cgcontextref of the layer, Then be copied to the screen

6. Custom Layer

A. Create a new class Aacalayer that inherits from Calayer, and then overwrite the Drawincontext: method, Draw inside

1-(void) Drawincontext: (cgcontextref) ctx{3Cgcontextsetrgbfillcolor (CTX,0,1,0.5,1);//fill, appear solid5 //starting point7Cgcontextmovetopoint (CTX, -,0);9 //Connect from (50, 0) to (0, +) OneCgcontextaddlines (CTX,0, -); - //Connect from (0, 100) to (+) theCgcontextaddlinetopoint (CTX, -, -); - //Draw Path + Cgcontextclosepath (CTX); -  +}

Each time you create a aacalayer, a triangle is drawn

1Aalayer *layer =[Aalayer layer];2  //Setting the width height of the layer3Layer.bounds = CGRectMake (0,0, -, -);4  //set the location of the layer5Layer.position = Cgpointmake ( -, -);6  //Start Drawing Layers7[Layer Setneedsdisplay];//writing this method will automatically trigger the Drawincontext: Method8[Self.view.layer Addsublayer:layer];

B. Do not want to write subclasses, you can also directly use the agent can create a new layer, Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX in the drawing, but cannot set UIView as its agent, Otherwise, there is a conflict with UIView, because UIView is already the degegate of the internal root layer.

1Calayer *layerr =[Calayer layer];2   //Setting the width height of the layer3Layerr.bounds = CGRectMake (0,0, -, -);4   //set the location of the layer5Layerr.position = Cgpointmake ( -, -);6Laere.Delegate-Self ;7   //Start Drawing Layers8[Layer setneedsdisplay];//no matter which method you take to customize the layer, you must call Calayer's Setneedsdisplay method to draw properly.9 [Self.view.layer Addsublayer:layer];Ten  One //in the Proxy method A- (void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX { -      //Set Color -Cgcontextsetrgbstrokecolor (CTX,0,0,1,1);//This does not fill, hollow the      //Set Border Width -Cgcontextsetlinewidth (CTX,5); -       -      //Place a rectangle in a path + Cgcontextaddrect (CTX, layer.bounds); -       +      //Draw Path A Cgcontextstrokepath (CTX); at}

iOS development-calayer and UIView detailed summary

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.