iOS core note--calayer-basic usage

Source: Internet
Author: User

1, Calayer simple use:

※了解:By manipulating the Calayer object, you can easily adjust some of the interface properties of the UIView, such as: Shadow, fillet size, border width ... Before IOS 7.0 using Calayer requires manual import QuartzCore框架 .

2, modify the interface properties of UIView by Calayer: 1, drag into UIView in storyboard, and set related properties:

as shown in the following:

2. Set the Shadow:
1.  // 1. 设置阴影颜色
2. self.blueView.layer.shadowColor = [UIColor blackColor].CGColor;
3. // 2. 设置阴影偏移量, width: 向右为正, height: 向下为正.
4. self.blueView.layer.shadowOffset = CGSizeMake(-20, -20);
5. // 3. 设置阴影不透明度, 0: 不显示阴影; 1: 显示阴影. 必须设置该属性才能显示阴影
6. self.blueView.layer.shadowOpacity = 1;

第1行: Set the color of the shadow to black, note that the Uicolor Cgcolor attribute is used here, is a type of CGCOLORREF data;

第2行: Sets the offset size of the shadow to the upper left corner of the original image.

第3行: Set shadow opacity, 0: No shadows, 1: Show shadows, you must set this property to show shadows.

※重要: You must set the view when setting the shadow, masksToBounds = NO otherwise the shadow cannot be displayed.

Sample effect Diagram :

3. Set the fillet size:
1.  // 3. 切圆角, 不需要裁剪也能显示圆角
2. self.blueView.layer.cornerRadius = 10;

第1行: Sets the radius of the fillet to 10;

※重要: UIView setting rounded corners is not required, masksToBounds = YES because UIView has only one layer inside the default, i.e. the primary layer, so the main layer fillet radius is set directly.

Sample effect Diagram :

4. Set the border width and color:
1.  // 1. 设置边框颜色, 单独设置无效; 必须结合边框宽度
2. self.blueView.layer.borderColor = [UIColor redColor].CGColor;
3. // 2. 设置边框宽度
4. self.blueView.layer.borderWidth = 5;

第1行: Set border color to red (note: Cgcolor type);

第2行: Set border width to 8;

备注: Sets the border for UIView, the border does not occupy the content area, and the border is outside the UIView content area.

Sample effect Diagram :

3, modify the interface properties of Uiimageview through Calayer: 1. Drag the Uiimageview into the storyboard and set the related properties:

as shown in the following:

2. Set the Shadow:
1.    // 1. 设置阴影
2. self.imageView.layer.shadowColor = [UIColor blackColor].CGColor;
3. // 2. 设置阴影偏移量
4. self.imageView.layer.shadowOffset = CGSizeMake(-20, -20);
5. // 3. 显示阴影
6. self.imageView.layer.shadowOpacity = 1;

第1行: Set the color of the shadow to black, note that the Uicolor Cgcolor attribute is used here, is a type of CGCOLORREF data;

第2行: Sets the offset size of the shadow to the upper left corner of the original image.

第3行: Sets the opacity of the shadow to 1, which means it is completely opaque, and if 0.5, it is semi-transparent.

Example :

3. Set the fillet size:
1.    // 1. 设置圆角
2. self.imageView.layer.cornerRadius = 20;
3. // 2. 超出主layer裁剪
4. self.imageView.layer.masksToBounds = YES;

第1行: Set the fillet radius to 20;

第2行: masksToBounds = YES : Set this property to YES crop the child layer beyond the main layer, Uiimageview, 子layer显示图片 but cut fillets 所有操作都是在主layer上 , so, even if we set the main layer fillet effect, If the Maskstobounds = YES is not set, the sub-layer is not cut out, and the ImageView has no fillet effect;

重要: masksToBounds = YES after setting, the shadow does not have a shadow effect.

Sample effect Diagram :

4. Set the border width and color:
1.    // 1. 设置边框, 默认为黑色, 边框占据imageView的内容部分
2. self.imageView.layer.borderWidth = 8;
3. // 2. 设置边框颜色
4. self.imageView.layer.borderColor = [UIColor redColor].CGColor;

备注:Set the border for Uiimageview 其边框内容是显示在UIImageView里面的内容区域 ;

第1行: Set border width to 8;

第2行: Sets the border color to red (note: Cgcolor type).

Sample effect Diagram :

5, set the deformation (CATransform3D):
  1.//1. Mode one: Use the Transform-3d method 
2. Self.imageView.layer.transform = catransform3dmakerotation (m_pi_4, 0, 0, 1);
3.//1-1. Compare transform-2d
4. Self.imageView.transform = Cgaffinetransformmakescale (0.5, 1);
5.
6. 2. Mode two: Using KVC, specify the transform type
7 in the value. Nsvalue *transform = [Nsvalue valuewithcatransform3d:catransform3dmakescale (2.0, 1.0, 0)];
8. [Self.imageView.layer setvalue:transform forkeypath:@ "transform"];
9.
10. 3. Method Three: Use KVC, specify transform type
11 in Forkeypath. [Self.imageView.layer setvalue:@ (0.5) forkeypath:@ "transform.scale.x"];
[Self.imageView.layer setvalue:@ (M_pi_4) forkeypath:@ "Transform.rotation"];

备注:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)参数

1, the use of transform properties can be set to rotate, zoom, translation and other effects;

2, M_pi_4: Four parts of π, clockwise rotation of 45°;

3, the following (0,0,1) indicates the rotation of the reference vector (i.e., rotation around the z-axis vector), modify the reference vector can do 3D rotation effect;

4, 函数含义 : Layer around the z axis clockwise rotation 45 °, that is, in the X, y plane to rotate.

Sample effect Diagram :

备注-1:Calayer uses the Catransform coordinate system:

备注-2:Calayer Modify the key of the catransform using the KVC form:

Note: Search in Apple's official documentation to “CATransform3D Key Paths” find out how to modify the layer's transform using KVC.

4, Calayer Animation: 4-1, pause Animation:

4-2. Start again:

iOS core note--calayer-basic usage

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.