iOS Development-Calayer layers

Source: Internet
Author: User

Basic use of Calayer

In iOS. What you can see and touch is basically uiview. For example, a button, a text label, a text input box, an icon, and so on. These are all UIView.

The fact that UIView can be displayed on the screen is entirely due to a layer inside it

When you create a UIView object, UIView internally creates a layer (that is, the Calayer object). You can access this layer by using the layers property of the UIView
@property (Nonatomic,readonly,retain) Calayer *layer;

When the UIView needs to be displayed on the screen, the DrawRect: method is called to paint, and the whole content is drawn on its own layer, after drawing, the system will copy the layer to the screen, so it is finished UIView display

In other words, the UIView itself does not have the function of the display, it is the inner layer of the display function

By manipulating the Calayer object, you can easily adjust some of the appearance properties of the UIView, for example:
Shadow
Fillet size
Border width and color
... ...

You can also animate layers to achieve some cool effects.

Properties of the Calayer
//width and height   @property  cgrect  bounds; //position (the default is the midpoint.) Detailed by Anchorpoint)   @property  cgpoint  Position //Anchor Point (x, y range is 0-1). Determines the meaning of position   @property  cgpoint  Anchorpoint; //background color (cgcolorref type)   @property  Cgcolorref BackgroundColor; //deformation properties   @property  Catransform3d transform;  
//边框颜色(CGColorRef类型)@property CGColorRef borderColor;//边框宽度@propertyCGFloat borderWidth;//圆角半径@propertyCGFloat cornerRadius;//内容(比方设置为图片CGImageRef)@propertyid contents;
The doubts about Calayer

First of all
Calayer is defined in the Quartzcore framework (Core Animation)
Cgimageref, cgcolorref Two types of data are defined in the Coregraphics framework.
Uicolor, uiimage are defined in the Uikit framework.

Secondly
The Quartzcore framework and 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.

In order to ensure portability, Quartzcore cannot use UIImage, Uicolor, can only use Cgimageref, Cgcolorref

Choice of UIView and Calayer

By Calayer, you can make the same interface effect as UIView.

Since Calayer and UIView all can achieve the same display effect, then who should choose who is good?
In fact, the control Calayer,uiview has more than one event-handling function.

Other words. Calayer cannot handle user touch events. and UIView can
So, assuming that what is shown needs to interact with the user, use UIView, assuming that there is no need to interact with the user, using UIView or Calayer to
Of course. The performance of Calayer is higher because it is less capable of event handling and more lightweight

Position and Anchorpoint Introduction
//CALayer有2个非常重要的属性:position和anchorPoint@propertyCGPoint position;用来设置CALayer在父层中的位置以父层的左上角为原点(00)@propertyCGPoint anchorPoint;称为“定位点”、“锚点”决定着CALayer身上的哪个点会在position属性所指的位置以自己的左上角为原点(00)它的x、y取值范围都是0~1,默认值为(0.50.5),意味着锚点在layer的中间
Anchorpoint ()

Position and Anchorpoint

Implicit animations

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

The entire non-root Layer. This is the manually created Calayer object. There's an implicit animation.

What is an implicit animation?
When changes are made to some properties of a non-root layer. The default is to create some animations on your own
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. Changing this property will produce a zoom animation
BackgroundColor: Used to set the background color of the calayer. Changing this property will result in a gradient animation of the background color
Position: The location to set the Calayer. Changing this property will create a panning animation

begin];commit];
Calayer Layer Instances
 @interface viewcontroller ()@property(Weak,nonatomic)Iboutlet UIView*redview;@property(Weak,nonatomic)Iboutlet Uiimageview*imageview;@end @implementation viewcontroller - (void) viewdidload{[SuperViewdidload];} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[UIViewAnimatewithduration:1animations:^{//Zoom_imageview. Layer. Transform= Catransform3dmakerotation (M_PI,1,1,0);//Panning_imageview. Layer. Transform= Catransform3dmaketranslation ( $, $,0);//Zoom_imageview. Layer. Transform= Catransform3dmakescale (1,0.5,1);//using KVC to change the deformationNsvalue *rotation = [Nsvalue valuewithcatransform3d:catransform3dmakerotation (M_PI,1,1,0)]; [_imageview. LayerSetvalue:rotation forkeypath:@"Transform"]; [_imageview. LayerSetValue: @M_PI forkeypath:@"Transform.rotation"]; [_imageview. Layersetvalue:@0.5forkeypath:@"Transform.scale"];//Panning X-axis[_imageview. Layersetvalue:@ $forkeypath:@"Transform.translation.x"]; }];} - (void) imagelayer{//Round cut_imageview. Layer. Cornerradius= -;//Trim all out of layer borders_imageview. Layer. Maskstobounds=YES; _imageview. Layer. BorderColor= [UicolorWhitecolor]. Cgcolor; _imageview. Layer. BorderWidth=2;} - (void) viewlayer{//Set Shadow transparency_redview. Layer. Shadowopacity=1;//Set shadow color_redview. Layer. Shadowcolor= [UicolorYellowcolor]. Cgcolor;//Set shadow fillet radius_redview. Layer. Shadowradius=Ten;//Set the fillet radius_redview. Layer. Cornerradius= -;//Set Border radius_redview. Layer. BorderColor= [UicolorWhitecolor]. Cgcolor;//Set Border radius_redview. Layer. BorderWidth=2;}@end
Define your own coating
 @interface viewcontroller ()@end @implementation viewcontroller - (void) viewdidload{[SuperViewdidload];//Create a layerCalayer *layer = [Calayer layer];//Set dimensionsLayer. Bounds= CGRectMake (0,0, -, -);//Set LocationLayer. Position= Cgpointmake ( -, -);//Set colorLayer. BackgroundColor= [UicolorRedcolor]. Cgcolor;//Set contentLayer. Contents= (__bridgeID)[UIImageimagenamed:@"Picture.png"]. Cgimage; [ Self. View. LayerAddsublayer:layer];}@end

iOS Development-Calayer 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.