IOS-The simple use of calayer

Source: Internet
Author: User

First, Brief introduction

In iOS, what you can see and touch is basically uiview, such as a button, a text tag, an icon, and so on, all of which are uiview.

In fact, UIView can be displayed on the screen entirely because of a layer inside it, when creating UIView objects, a layer (that is, Calayer object) is automatically created inside the UIView, which can be accessed through the Layer property of UIView.

When UIView needs to be displayed to the screen, the DrawRect: method is called to draw, and all the content is drawn on its own layer, and after the drawing is finished, the layer is copied to the screen, so the UIView is displayed.

In other words, the UIView itself does not have a display function, and the display function is his inner layer.

Second, simple use

UIView can be displayed entirely because of the internal Calayer object. Therefore, by manipulating this Calayer object, you can easily adjust some of the properties of the interface, such as shadow, fillet size, border width and color.

Create a new project and add a view to the storyboard

1. Set the width and color of the border through the layer

    #import "ViewController.h"            @interface viewcontroller ()            @property (weak, nonatomic) Iboutlet UIView * CustomView;            @end            @implementation Viewcontroller            -(void) viewdidload {          [super viewdidload];    set the border width to          self.customView.layer.borderWidth =;    set Border color          self.customView.layer.borderColor = [Uicolor Cyancolor]. Cgcolor;                          }            @end  

2. Set the border to rounded corners through layer

    sets the fillet of the layer      Self.customView.layer.cornerRadius = 20;

3. Add a picture to the layer

    #import "ViewController.h"            @interface viewcontroller ()            @property (weak, nonatomic) Iboutlet UIView * CustomView;            @end            @implementation Viewcontroller            -(void) viewdidload {          [super viewdidload];    set the border width to ten          self.customView.layer.borderWidth = ten;    set Border color          self.customView.layer.borderColor = [Uicolor blackcolor]. Cgcolor;    set the fillet of layer          Self.customView.layer.cornerRadius =;    Adding a image,contents on the view's layer means accepting the content          self.customView.layer.contents = (ID) [UIImage imagenamed:@ "1"]. cgimage;      }            @end  

Description: Contents is the ID type, can accept the content, the above instance let layer display a picture, careful observation can find four rounded corners of the part of the open a corner out.

Cause:

The root layer on the CustomView

Layers of UIImage

After adding

That's because the image you set is not displayed on the main layer, but on the sublayer. You can cut out the part of the main layer by setting a range.

#import "ViewController.h"    @interface viewcontroller ()    @property (weak, nonatomic) Iboutlet UIView * CustomView;    @end    @implementation Viewcontroller    -(void) viewdidload {      [super viewdidload];    set the border width to      self.customView.layer.borderWidth = ten;    set Border color      self.customView.layer.borderColor = [Uicolor blackcolor]. Cgcolor;    set the fillet of layer      Self.customView.layer.cornerRadius =;    set the portion of the child layer minus  //    First: The method used in the UI Framework  //    self.customView.clipsToBounds = YES;    The second way: the usual way      self.customView.layer.masksToBounds = YES;    Adding a image,contents on the view's layer means accepting the content      self.customView.layer.contents = (ID) [UIImage imagenamed:@ "1"]. cgimage;        }    @end

4. Setting shadows

Set the shadow, not only the shadow color, but also the offset and transparency of the shadow.

Because if the offset is not set, then the shadow and layer are completely overlapping, and the default transparency is 0 (that is, completely transparent

    set Shadow color      Self.customView.layer.shadowColor = [Uicolor blackcolor]. Cgcolor;    sets the offset of the shadow, if positive, to the right offset      self.customView.layer.shadowOffset = Cgsizemake (5);    sets the opacity of the shadow      self.customView.layer.shadowOpacity = 0.6;

Note: If you set a part that exceeds the main view, setting the shadow does not show the effect

    -(void) viewdidload {          [super viewdidload];    set the border width to          self.customView.layer.borderWidth = ten;    set Border color          self.customView.layer.borderColor = [Uicolor blackcolor]. Cgcolor;    set the fillet of layer          Self.customView.layer.cornerRadius =;    set the portion of the child layer minus      //    First: The method used in the UI Framework      //    self.customView.clipsToBounds = YES;    The second way: the usual way          self.customView.layer.masksToBounds = YES;    Adding a image,contents on the view's layer means accepting the content          self.customView.layer.contents = (ID) [UIImage imagenamed:@ "1"]. Cgimage;    set Shadow color          Self.customView.layer.shadowColor = [Uicolor blackcolor]. Cgcolor;    sets the offset of the shadow, if positive, to the right offset          self.customView.layer.shadowOffset = Cgsizemake (5);    sets the opacity of the shadow          self.customView.layer.shadowOpacity = 0.6;      }

5, as long as it is inherited from the UIView has a layer attribute

Add a new picture in storyboard

    -(void) viewdidload {          [super viewdidload];    sets the color and width of the picture border          self.imageView.layer.borderColor = [Uicolor blackcolor]. Cgcolor;          Self.imageView.layer.borderWidth = 5;    set the fillet of layer          Self.imageView.layer.cornerRadius =;    set the portion of the layer beyond the sublayer to trim off          self.imageView.layer.masksToBounds = YES;                

6, set the Bounds property, in the settings is necessary to remove the Stroyboard in the automatic layout properties

Set the deformation properties of a picture (transform)

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {  //    via UIView settings (2D effect)      self.imageView.transform = cgaffinetransformmaketranslation (0, -100);    set by layer (3D effect)      self.imageView.layer.transform = catransform3dmaketranslation (100, 20, 0);  

Setting 2D effects with UIView

Set the 3D effect through a layer

Using KVC settings

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {  //    via UIView settings (2D effect)    self.imageView.transform = cgaffinetransformmaketranslation (0, -100);    set by layer (3D effect)  //    self.imageView.layer.transform = catransform3dmaketranslation (0, +);    set      nsvalue *value by KVC = [Nsvalue valuewithcatransform3d:catransform3dmaketranslation (0)];      [Self.imageView.layer setvalue:value forkeypath:@ "transform"];    If you just set a move in one direction, you can refer to the following code      [Self.imageView.layer setvalue:@ ( -100) forkeypath:@ " Transform.translation.x "];  }

Set rotation

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {  //    via UIView settings (2D effect)    self.imageView.transform = cgaffinetransformmaketranslation (0, -100);    set by layer (3D effect)  //    self.imageView.layer.transform = catransform3dmaketranslation (0, +);    set by KVC  //    Nsvalue *value = [Nsvalue valuewithcatransform3d:catransform3dmaketranslation (100, 20, 0)];    [Self.imageView.layer setvalue:value forkeypath:@ "transform"];    If you just set a move in one direction, you can refer to the following code  //    [Self.imageView.layer setvalue:@ ( -100) forkeypath:@ " Transform.translation.x "];    rotation      self.imageView.layer.transform = catransform3dmakerotation (M_pi_4, 1, 1, 0.5);        

IOS-The simple use of 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.