Two ways to customize layers for iOS development-layers

Source: Internet
Author: User

To customize a layer, you only need to build a class to inherit the Calayer method

There are two ways to make a custom layer initialize with a drawing on it.

Overriding the Drawincontext method

Customize the layer below the method, and then you must customize the Layer object to display the call
[Layer Setneedsdisplay] Before this method is called;

customizing layer Files

- (void)drawInContext:(CGContextRef)ctx {    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);//设置红色    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));//画圆    CGContextFillPath(ctx);}

Controller files

Testlayer *layer = [Testlayer layer];Layer. Bounds= CGRectMake (0,0, -, -);Layer. Position= Cgpointmake ( -, -);Layer. Anchorpoint= Cgpointmake (0,0);Layer. BackgroundColor= [Uicolor Bluecolor]. Cgcolor;Layer. Cornerradius=Ten;[Layer Setneedsdisplay]or//must be shown to call this method before calling the method above[Self. View. LayerAddsublayer:layer];
Using proxies

First the agent controller does not need to follow the protocol, because this Protocol is a classification, all the NSObject are implemented

Method of implementing agent for controller

#pragma marks -实现代理的方法- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));    CGContextFillPath(ctx);}

Call

Testlayer *layer = [Testlayer layer];Layer. Bounds= CGRectMake (0,0, -, -);Layer. Position= Cgpointmake ( -, -);Layer. Anchorpoint= Cgpointmake (0,0);Layer. BackgroundColor= [Uicolor Bluecolor]. Cgcolor;Layer. Cornerradius=Ten;Layer. Delegate= Self;[Layer Setneedsdisplay];//must display the call, or the proxy method will not be invoked[Self. View. LayerAddsublayer:layer];

Two ways to customize layers for iOS development-layers

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.