IOS-draw layer-CALayer attributes, ios-draw image-calayer

Source: Internet
Author: User

IOS-draw layer-CALayer attributes, ios-draw image-calayer

 

I. position and anchorPoint

1. Brief Introduction

CALayer has two important attributes: position and anchorPoint.

@ Property CGPoint position;

Used to set the location of CALayer in the parent layer

Starting from the upper left corner of the parent layer (0, 0)

 

@ Property CGPoint anchorPoint;

It is called "positioning point" and "anchor"

Determines the position indicated by the position attribute of CALayer.

Use your own upper left corner as the origin (0, 0)

The value range of x and y is 0 to 0 ~ 1. The default value is (0.5, 0.5)

2. Illustration

AnchorPoint

Its value ranges from 0 ~ 1

 

The anchorPoint of the red layer is (0, 0)

The anchorPoint of the red layer is (0.5, 0.5)

The anchorPoint of the red layer is)

The anchorPoint of the red layer is (0.5, 0)

Position and anchorPoint

Add a red layer to the green layer. The position of the red layer is determined by the position attribute.

Assume that the position of the red layer is (100,100)

Which point of the red layer is moved to the Coordinate Position (100,100) and the anchor point.

The anchor of the red layer is (0, 0)

The anchor of the red layer is (0.5, 0.5)

The anchor of the red layer is)

The anchor of the red layer is (0.5, 0)

3. Sample Code

(1) No anchor is set. The default anchor position is (0.5, 0.5)

1 // 2 // YYViewController. m 3 // 03-attributes such as anchor 4 // 5 // Created by apple on 14-6-21. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 13 @ end14 15 @ implementation YYViewController16 17-(void) viewDidLoad18 {19 [super viewDidLoad]; 20 // create layer 21 CALayer * layer = [CALayer layer]; 22 // set layer attribute 23. backgroundColor = [UIColor redColor]. CGColor; 24 layer. bounds = CGRectMake (0, 0,100,100); 25 // Add layer 26 [self. view. layer addSublayer: layer]; 27 28} 29 30 @ end

Display Effect:

(1) set the anchor position to (0, 0)

1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 // create layer 5 CALayer * layer = [CALayer layer]; 6 // set layer attributes 7. backgroundColor = [UIColor redColor]. CGColor; 8 layer. bounds = CGRectMake (0, 0,100,100); 9 // set the anchor to (0, 0) 10 layer. anchorPoint = CGPointZero; 11 // Add layer 12 [self. view. layer addSublayer: layer]; 13} 14 @ end

Display Effect:

Ii. Implicit Animation

1. Simple Description

Each UIView is associated with a CALayer by default. We can call this Layer as the Root Layer)

All non-Root layers, that is, manually created CALayer objects, all have implicit animations.

What is implicit animation?

When some attributes of a non-Root Layer are modified, some animation effects are automatically generated by default.

These attributes are called animation Properties)

 

List several common Animatable Properties:

Bounds: used to set the width and height of CALayer. Modifying this attribute will produce a scaling animation.

BackgroundColor: used to set the background color of CALayer. Modifying this attribute will produce a gradient animation of the background color.

Position: used to set the location of CALayer. Modifying this attribute produces a translation animation.

2. Sample Code

1 // 2 // YYViewController. m 3 // 04-implicit animation 4 // 5 // Created by apple on 14-6-21. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 @ property (nonatomic, strong) CALayer * layer; 13 @ end14 15 @ implementation YYViewController16 17-(void) viewDidLoad18 {19 [super viewDidLoad]; 20 // create layer 21 CALayer * mylayer = [CALayer layer]; 22 // set layer attribute 23 mylayer. backgroundColor = [UIColor brownColor]. CGColor; 24 mylayer. bounds = CGRectMake (0, 0,150,100); 25 // display position 26 mylayer. position = CGPointMake (100,100); 27 mylayer. anchorPoint = CGPointZero; 28 mylayer. cornerRadius = 20; 29 // Add layer 30 [self. view. layer addSublayer: mylayer]; 31 self. layer = mylayer; 32} 33 34-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event35 {36 // implicit animation 37 self. layer. bounds = CGRectMake (0, 0,200, 60); 38 self. layer. backgroundColor = [UIColor yellowColor]. CGColor; 39} 40 @ end

Effect:

Disable implicit Animation:

1 [CATransaction begin]; 2 [CATransaction setDisableActions: YES]; 3 // implicit animation 4 self. layer. bounds = CGRectMake (0, 0,200, 60); 5 self. layer. backgroundColor = [UIColor yellowColor]. CGColor; 6 [CATransaction commit];

3. How can I check whether implicit animation is supported for an attribute of CALayer?

You can check the header file to see if there is any Animatable. If yes, it indicates support.

You can also view the official documentation

All the attributes marked in this document support implicit animation.

 

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.