Core Animation Calayer

Source: Internet
Author: User

1. Let's take a brief look at the basic concepts of Calayer:

Calayer is the basis of the core animation, can do round corners, shadows, borders and other effects;

Each uiview has a layer attribute inside it;

UIView can respond to events, while Calayer is only responsible for display;

When the core animation is implemented, it is essentially converting the contents of the Calayer into bitmaps, thus facilitating the manipulation of the graphics hardware;

coordinate system of 2.CALayer

UIView has frame, bounds, and center three properties

Calayer also have similar properties, namely frame, bounds, position, anchorpoint

3. Anchor Point

The default value for Anchorpoint is (0.5,0.5), which is the anchorpoint default at the center point of the layer.

Position is the position coordinates of the anchorpoint in the layer in the Superlayer.

frame.origin.x = Position.x-anchorpoint.x * bounds.size.width;

FRAME.ORIGIN.Y = Position.y-anchorpoint.y * bounds.size.height;

Common Properties of 4.CALayer

Background color, content, border, shadow, transparency

5. Tips


Layer is not designed to replace views, so you cannot create a separate visual component based on Calayer

Layer is designed to provide basic visual content of the view, thus improving the efficiency of animation execution

In addition to providing visual content, layer is not responsible for the view's event response, content rendering, etc., and the layer cannot participate in the responder chain

6. Refer to the following code to better understand some of the basic properties and usage of Calyer

-(void) Viewdidload {

[Super Viewdidload];

UIView *view = [[UIView alloc]initwithframe:cgrectmake (20, 20, 200, 300)];

View.backgroundcolor = [Uicolor Redcolor];

[Self.view Addsubview:view];


Border (extends to the interior of the view)

View.layer.borderwidth= 10;//Default is 0

View.layer.borderColor = [Uicolor Greencolor]. Cgcolor;

Fillet radius

View.layer.cornerRadius = 50;


Shadow color (Default black)

View.layer.shadowColor = [Uicolor Graycolor]. Cgcolor;

Shadow offset (default is 0 0)

View.layer.shadowOffset = Cgsizemake (-50, 50);

Opacity of the shadow (default is 0 transparent)

view.layer.shadowOpacity = 1;


__bridge:core Foundation Object (cf core text) [gives memory management of CF object to ARC processing]

View.layer.contents = (__bridge ID _nullable) ([UIImage imagenamed:@ "1.jpeg"]. Cgimage);

Manual Management

Cgimageref cgimage = [UIImage imagenamed:@ "1.jpg"]. Cgimage;

Cgimagerelease (Cgimage);

The part of the child view that is out of bounds does not appear

View.clipstobounds = YES;

Parts beyond layer are not displayed (crop shadows off)

View.layer.masksToBounds = YES;


Core Animation Calayer

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: 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.