iOS Development UI Chapter-calayer properties of the layer

Source: Internet
Author: User

iOS Development UI Chapter-calayer properties of the layer

First, position and Anchorpoint

1. Brief introduction

Calayer has 2 very important properties: position and Anchorpoint

@property cgpoint position;

Used to set the position of the Calayer in the parent layer

The upper left corner of the parent layer is the origin (0, 0).

@property Cgpoint Anchorpoint;

Known as Anchor Point, Anchor Point

Determines which point of the calayer is in the position indicated by the position attribute

In its own upper left corner as the origin (0, 0)

Its x, y range is 0~1, the default value is (0.5, 0.5)

2. Illustration

Anchorpoint

It has a value of 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, where the red layer appears, determined by the Position property

Suppose the position of the red layer is (100,100)

Exactly which point of the red layer is moved to (100,100) The coordinate position, the anchor point.

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

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

The anchor point of the red layer is (

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

3. Code examples

(1) No anchor points are set. The default anchor point location is (0.5,0.5)

1//2//  YYVIEWCONTROLLER.M 3//  03-anchor points and other attributes 4//5//  Created by Apple on 14-6-21.6//  Copyright (c) 2014 Itcase. All rights reserved. 7//8  9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 {     [super viewdidload];20     //Create layer     calayer *layer=[calayer layer];22     Set the properties of a layer     Layer.backgroundcolor=[uicolor Redcolor]. cgcolor;24     layer.bounds=cgrectmake (0, 0, +); +     //Add layer     [Self.view.layer addsublayer:layer];     }29 @end

Display effect:

(1) Set Anchor point position as (0,0)

1-(void) Viewdidload 2 {3     [Super Viewdidload]; 4     //Create layer 5     calayer *layer=[calayer layer]; 6     //Set properties of the layer 7< C4/>layer.backgroundcolor=[uicolor Redcolor]. Cgcolor; 8     layer.bounds=cgrectmake (0, 0, +); 9     //Set the anchor point to (0,0) ten     layer.anchorpoint=cgpointzero;11     // Add a layer     [Self.view.layer addsublayer:layer];13}14 @end

Display effect:

Second, implicit animation

1. Brief description

Each uiview is associated with a calayer, and we can call this layer the root layer (root)

All non-root layers, which are manually created Calayer objects, have implicit animations

What is an implicit animation?

When modifying some properties of a non-root layer, some animations are automatically generated by default

These properties are called Animatable properties (animated property)

Several common animatable Properties are listed:

Bounds: Used to set the width and height of the calayer. Modifying this property will result in a scaled animation

BackgroundColor: Used to set the background color of the calayer. Modifying this property will result in a gradient animation of the background color

Position: The location to set the Calayer. Modifying this property will result in a panning animation

2. Code examples

 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" @interface Yyviewcontroller () @property (nonatomic,strong) Calayer *layer;13 @end14 @implementation YYViewController16-(void) viewDidLoad18 {[Super VIEWDIDLOAD];20//Create layer CALa Yer *mylayer=[calayer layer];22//Set layer properties at Mylayer.backgroundcolor=[uicolor Browncolor].     Cgcolor;24 mylayer.bounds=cgrectmake (0, 0, 150, 100); 25//Display Position: mylayer.position=cgpointmake (100, 100); 27 MYLAYER.ANCHORPOINT=CGPOINTZERO;28 mylayer.cornerradius=20;29//Add layer [Self.view.layer addsublayer:mylayer];     Self.layer=mylayer;32}33-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event35 {36//Implicit animation 37 Self.layer.bounds=cgrectmake (0, 0, max), Self.layer.backgroundcolor=[uicolor Yellowcolor]. cgcolor;39}40 @end 

Effect:

To close an implicit animation:

1     [catransaction begin];2     [catransaction setdisableactions:yes];3     //Implicit animation 4     self.layer.bounds= CGRectMake (0, 0, max), 5     self.layer.backgroundcolor=[uicolor Yellowcolor]. Cgcolor;6     [Catransaction commit];

3. How do I see if a property of Calayer supports implicit animation?

Can look at the header file, see if there is animatable, if there is support.

You can also view official documents

The attributes indicated in the document are those that support implicit animation.

iOS Development UI Chapter-calayer properties of the layer

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.