Detailed explanation of ios sdk CALayer (1) and iossdk calayer

Source: Internet
Author: User

Detailed explanation of ios sdk CALayer (1) and iossdk calayer

Original Blog, reprinted, please indicate the source

Http://blog.csdn.net/hello_hwc? Viewmode = contents


This series updates Core Animation, but CALayer is the basis of Core Animation.

What is a CALayer?

From the official website-Layers Provide the Basis for Drawing and Animations (Layers is the Basis for Drawing and animation)

Layer is a 2D plane in 3D space. Layer management information, such as rotate, transfrom, content (image, etc.), and visual attributes (backgroundColor, alpha. Layer maintains its state information by managing bitmap. In this regard, Layer can be seen as an object model because it is mainly used to manage data.


Layer is based on bitmap. It captures the content to be presented by the View and caches the content in a bitmap. This bitmap can be considered as an object. In this way, each operation, such as pan rotation, is only a bitmap matrix operation. Layer-based animation process



Because Layer-based rendering processes static Bitmap, while bitmap is good at GPU, it is much more efficient than View-based rendering, because the drawRect call must be re-drawn every time based on The View.

Two layers support inheritance, support adding sublayers, and support Layer adjustment of sublayers

Common Layer subclass

CAEmitterLayer

Transmitter layer, used to control particle effect

CAGradientLayer

Gradient layer, color gradient

CAEAGLayer

Layers drawn using OpenGL ES

CAReplicationLayer

Used to automatically copy sublayer

CAScrollLayer

Used to manage slide areas

CAShapeLayer

Draw a three-dimensional besell Curve

CATextLayer

AttributeString can be drawn

CATiledLayer

Used to manage a split big chart

CATransformLayer

Used to render the layered structure of a 3D layer

Several functions used to manage Layer content

AddSublayer:

insertSublayer:above:

insertSublayer:atIndex:

insertSublayer:below:

RemoveFromSuperlayer

ReplaceSublayer::


3. directly set the Layer of the UIView

First, let's look at an example. Then, I will list common attributes and analyze several attributes that are not easy to understand.

Drag a UIView on the Stroyboard, and control + drag generates an IBOutlet named containView.

@property (weak, nonatomic) IBOutlet UIView *containView;
Then, in ViewDidLoad, type the following code

ContainView. layer. backgroundColor = [UIColor lightGrayColor]. CGColor; // inview the background color. layer. cornerRadius = 20.0; // rounded corner containView. layer. shadowColor = [UIColor blueColor]. CGColor; // shadow color containView. layer. shadowOpacity = 0.8; // shadow transparency containView. layer. shadowOffset = CGSizeMake (3.0, 3.0); // shadow offset containView. layer. borderColor = [UIColor redColor]. CGColor; // border color containView. layer. borderWidth = 2; // Border Width

In this way, the effect after running


4. Add a Sublayer

   containView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;    containView.layer.cornerRadius = 20.0;    containView.layer.shadowColor = [UIColor blueColor].CGColor;    containView.layer.shadowOpacity = 0.8;    containView.layer.shadowOffset = CGSizeMake(3.0, 3.0);    containView.layer.borderColor = [UIColor redColor].CGColor;    containView.layer.borderWidth = 2;        CALayer * sublayer1 = [CALayer layer];    sublayer1.backgroundColor = [UIColor blueColor].CGColor;    sublayer1.frame = CGRectMake(0, 0,80,80);    sublayer1.anchorPoint = CGPointMake(0.5, 0.5);    sublayer1.position = CGPointMake(100,100);    [containView.layer addSublayer:sublayer1];


It is possible that when a Sublayer is added, the frame range of the sublayer has exceeded the frame of the super Layer. What will happen?

    sublayer1.position = CGPointMake(0,CGRectGetMaxY(containView.bounds)-10);
Modify the position of sublayer1 and then the effect

However, we often do not want the sublayer to be out of the super layer range. In this case, we can set this attribute.

    containView.layer.masksToBounds = YES;
Effect

I have heard of examples of two common CALayer subclasses UIShapeLayer and UITextLayer.

    CAShapeLayer * shapeLayer = [CAShapeLayer layer];    CGMutablePathRef path = CGPathCreateMutable();    CGPathMoveToPoint(path,nil,0.0,0);    CGPathAddLineToPoint(path,nil,0.0,CGRectGetHeight(containView.bounds)/2);    shapeLayer.path = path;    shapeLayer.bounds = CGRectMake(0,0,5.0,CGRectGetHeight(containView.bounds)/2);    shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);    shapeLayer.position = CGPointMake(CGRectGetMidX(containView.bounds),CGRectGetMidY(containView.bounds));    shapeLayer.lineWidth = 5.0;    shapeLayer.lineCap = kCALineCapRound;    shapeLayer.strokeColor = [UIColor yellowColor].CGColor;    [containView.layer addSublayer:shapeLayer];        CATextLayer * textLayer = [CATextLayer layer];    NSString * text = @"blog.csdn.net/hello_hwc";    NSAttributedString * attributeString = [[NSAttributedString alloc] initWithString:text];    textLayer.string = text;    textLayer.alignmentMode = @"center";    textLayer.fontSize = 12;    textLayer.foregroundColor = [UIColor brownColor].CGColor;    CGRect bounds;    bounds.origin = CGPointMake(0, 0);    bounds.size = attributeString.size;    textLayer.bounds = bounds;    textLayer.position = CGPointMake(100,100);    [containView.layer addSublayer:textLayer];


Five anchorPoint and position

Unlike UIView, a Layer is mainly set by three attributes (Frame is rarely used ):

Bounds-set the size

AnchorPoint-set the anchor point (the anchor point has a great impact on the subsequent layer animation)

Position-position of the anchor in the superLayer

This is a bit abstract. Let's take a look at the figure below to understand it.

For IOS, the two values of archPoint (x, y) in the coordinate system are usually 0.0-1.0. The default value is (0.5, 0.5). The value here can be seen as the proportion of x occupied, for example, the default value is 0.5, and 0.5 is in the middle of x and y.

Position indicates the position of the AnchorPoint in the super layer.

For example


Layer 5 display image


    CALayer * imageLayer = [CALayer layer];    imageLayer.bounds = CGRectMake(0,0,200,100);    imageLayer.position = CGPointMake(200,200);    imageLayer.contents = (id)[UIImage imageNamed:@"lichen.jpg"].CGImage;    imageLayer.contentsGravity = kCAGravityResizeAspect;    [containView.layer addSublayer:imageLayer];

 

Here, we will explain in detail the following contentGravity attribute. This attribute determines how to fill contents.

It can be divided into two aspects,

Aspect 1: Location

Details

Aspect 2: proportional Transformation

Differences between Layer 6 and UIView

From official documents

Layers are not a replacement for your app's views-that is, you cannot create a visual interface based solely on layer objects. layers provide infrastructure for your views. specifically, layers make it easier and more efficient to draw and animate the contents of views and maintain high frame rates while doing so. however, there are running things that layers do not do. layers do not handle events, draw content, participant in the responder chain, or do your other things. for this reason, every app must still have one or more views to handle those kinds of interactions.

In iOS, every view is backed by a corresponding layer object but in OS X you must decide which views shoshould have layers. in OS X v10.8 and later, it probably makes sense to add layers to all of your views. however, you are not required to do so and can still disable layers in cases where the overhead is unwarranted and unneeded. layers do increase your app's memory overhead somewhat but their benefits often outweigh the disadvantage, so it is always best to test the performance of your app before disabling layer support.

Simply put, the biggest difference between a View and a Layer is that a View can accept user input (such as a touch), but a Layer cannot. A Layer alone cannot present any visible content and must rely on a View. Layer is just something that is presented to users in a geometric way. It is lightweight and usually uses the Cache technology, which consumes less resources.

7. Next Plan

In the next article, we will continue to update CALayer, update some infrequently used attributes and methods, and then update Core Animation content.

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.