Introduction to iOS development Calayer

Source: Internet
Author: User

Calayer Introduction:
    • Calayer We call it a layer, the concept of the Calayer class is very similar to UIView and can include pictures, text, background colors, and so on. The biggest difference between it and UIView is that it doesn't respond to user interaction, so it doesn't know the existence of a response chain, and its API does not have the ability to respond, although it provides a "method for whether a point is in the layer's scope."

    • In each of the UIView instances, there is a default support layer Layer,uiview responsible for creating and managing this layer. In fact, UIView is able to display, because it has this layer, only has the function of the display, UIView is only a layer of its packaging, the implementation of Calayer delegate, provides the specific functions of handling event interaction, as well as the animation of the underlying method of advanced API. It can be said that Calayer is the internal implementation details of UIView.

The Layer property in UIView
  • When you create a UIView object, a layer (that is, the Calayer object) is automatically created inside UIView, which can be accessed through the Layer property of UIView.
    @property(nonatomic,readonly,retain) CALayer *layer;
  • Properties of the Calayer class

    • 注意:CALayer是定义在QuartzCore框架中的,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef

       宽度和高度@property CGRect bounds;位置(默认指中心点,具体由anchorPoint决定)@property CGPoint position;锚点(x,y的范围都是0-1),决定了position的含义@property CGPoint anchorPoint;背景颜色(CGColorRef类型)@property CGColorRef backgroundColor;形变属性@property CATransform3D transform;边框颜色(CGColorRef类型)@property CGColorRef borderColor;边框宽度@property CGFloat borderWidth;圆角半径@property CGColorRef borderColor;内容(比如设置为图片CGImageRef)@property(retain) id contents;
    • Operation layer changes UIView and uiimageview appearance

      @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UIView *yellowview; @property (weak, nonatomic) Iboutlet    Uiimageview *imageview; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Set the border (border width goes inside) Self.imageView.layer.borderColor = [Uicolor Redcolor].    Cgcolor;    Self.imageView.layer.borderWidth = 3; Set Shadow Self.imageView.layer.shadowColor = [Uicolor Bluecolor].    Cgcolor;    self.imageView.layer.shadowOpacity = 1;    The opacity of the shadow, the default is 0 Self.imageView.layer.shadowOffset = cgsizemake (-10, 10);    Self.imageView.layer.shadowRadius = 50;    The larger the more blurred//set the fillet radius Self.imageView.layer.cornerRadius = 50;    Content beyond the root layer is automatically clipped out.------Shadow will not coexist self.imageView.layer.masksToBounds = YES;    NSLog (@ "%@", self.imageView.layer.sublayers); NSLog (@ "%@", self.imageView.layer.contents);    ImageView's picture is stored in the contents of the layer. So setting the fillet radius of a layer does not affect the picture}-(void) uiviewlayer{//Set border (border width goes inside) Self.yellowView.layer.borderColor = [Uicolor Redcolor]. Cgcolor;    Self.yellowView.layer.borderWidth = 3; Set Shadow Self.yellowView.layer.shadowColor = [Uicolor Bluecolor].    Cgcolor;    self.yellowView.layer.shadowOpacity = 1;    The opacity of the shadow, the default is 0 Self.yellowView.layer.shadowOffset = cgsizemake (-10, 10);    Self.yellowView.layer.shadowRadius = 50; The larger the more blurred//set the fillet radius Self.yellowView.layer.cornerRadius = 50;}

      Different points:
      Note: The image in Uiimageview is not directly added to the layer. This is added to the contents in the layers layer.
      We set all the properties of the layer and it only acts on top of the layer. It doesn't work on contents. So we can't see the rounded effect of the picture.
      You want the image to have rounded corners. You can set the Maskstobounds property to Yes,
      When set to Yes, it cuts out everything beyond the root layer.

The Catransform3d property of Calayer
旋转,x,y,z 分别代表x,y,z轴.(如UIView的动画都是绕着Z轴变化的)self.imageView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);平移self.imageView.layer.transform = CATransform3DScale(self.imageView.layer.transform, 0.5, 0.5, 0);缩放self.imageView.layer.transform = CATransform3DTranslate(self.imageView.layer.transform, 50, 50, 0);

The properties can be set by KVC.

但是CATransform3DMakeRotation它的值,是一个结构体, 所以要把结构转成对象.NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 0, 0)];[_imageView.layer setValue:value forKeyPath:@"transform.scale"];

Later Forkeypath property values are not scrambled. The Apple document gives the relevant properties.


Snip20160309_2.pngcalayer's position and Anchorpoint

Position and Anchorpoint are the two properties of Calayer.
We have previously modified the position of a control in a way that can be changed over frame.
It is now possible to modify the position of the control using the position and Anchorpoint properties of Calayer.

These two properties are used in conjunction.

    1. Position: It is used to set the position of the current layer within the parent control.
      So it coordinates the origin. The upper-left corner of the parent control is the (0.0) point.
    2. Anchorpoint: It's the point of the Calayer, which is where the position property refers to.
      Anchorpoint It is the origin (0.0) in the upper left corner of the current layer
      Its value range is 0~1, its default in the middle is (0.5,0.5) position.
      anchorPoint又称锚点.就是把锚点定到position所指的位置.

They are used in combination. To modify the position of a control, we can set its position point.
After the setup is complete, the anchorpoint on the layer will automatically be set to the location of the position.

Implicit animations

Before you know what an implicit animation is, you need to understand what root and non-root layers are.
1. Root layer: The layer of the uiview that is automatically associated with it we call it root.
2. Non-root layer: a layer that you manually create, called a non-root layer.

Implicit animation is when the non-root layer of the partial properties (bounds, backgroundcolor, position, etc.) are modified, it will automatically produce some animation effect.
We call this default animation to be an implicit animation.

    • How do I cancel an implicit animation?
      The first thing to know is how the animation is done at the bottom. The bottom of the animation is packaged into a single transaction.
      What is a transaction?
      Many operations are bound together, and when these operations are completed, the next operation is performed.
      开启事务[CATransaction begin];设置事务没有动画[CATransaction setDisableActions:NO];设置隐式动画执行的时长[CATransaction setAnimationDuration:2];提交事务[CATransaction commit];


Li Xiaonan
Links: http://www.jianshu.com/p/42b98ca384b7
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Introduction to iOS development 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: 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.