iOS开发UI篇—CAlayer简介

iOS开发UI篇—CAlayer简介

iOS开发UI篇—CALayer简介
 
1、简单介绍

在iOS中,你能看得见摸得着的东西基本上都是UIView,好比一个按钮、一个文本标签、一个文本输入框、一个图标等等,这些都是UIView。 html

其实UIView之因此能显示在屏幕上,彻底是由于它内部的一个图层,在建立UIView对象时,UIView内部会自动建立一个图层(即CALayer对象),经过UIView的layer属性能够访问这个层 框架

@property(nonatomic,readonly,retain) CALayer *layer;  iview

当UIView须要显示到屏幕上时,会调用drawRect:方法进行绘图,而且会将全部内容绘制在本身的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,因而就完成了UIView的显示 布局

换句话说,UIView自己不具有显示的功能,拥有显示功能的是它内部的图层。 ui

 

2、简单使用 atom

  UIView之因此可以显示,彻底是由于内部的CALayer对象。所以,经过操做这个CALayer对象,能够很方便地调整UIView的一些界面属性,好比:阴影、圆角大小、边框宽度和颜色等。 spa

  新建一个项目,在storyboard中添加一个view. .net

 

  1.经过layer设置边框的宽度和颜色 code

#import "YYViewController.h"


@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;


@end


@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置边框的宽度为20
    self.customView.layer.borderWidth=20;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor greenColor].CGColor;
}


@end
orm

 

2.经过layer设置边框为圆角

1 //设置layer的圆角 2 self.customView.layer.cornerRadius=20;

3.在layer上添加一张图片 

#import "YYViewController.h"


@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;


@end


@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置边框的宽度为20
    self.customView.layer.borderWidth=5;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor blackColor].CGColor;
    
    //设置layer的圆角
    self.customView.layer.cornerRadius=20;
    
    //在view的图层上添加一个image,contents表示接受内容
    self.customView.layer.contents=(id)[UIImage imageNamed:@"me"].CGImage;
    
}
@end

说明:contents是id类型,能够接受内容,上面的实例让layer显示一张图片,仔细观察能够发现四个圆角的部分露了一个角出来。

产生的缘由说明:

               

customview上的根layer

  

UIimage的图层

   

添加后

那是由于设置的image不是展现在主图层上的,而是显示在子图层上的。能够经过设置一个范围,设置超出主图层的部分把它给剪切掉。

有如下两种方法,建议使用layer中的方法(第二种)self.customView.layer.masksToBounds=YES;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置边框的宽度为20
    self.customView.layer.borderWidth=5;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor blackColor].CGColor;
    
    //设置layer的圆角
    self.customView.layer.cornerRadius=20;
    //设置超过子图层的部分裁减掉
    //UI框架中使用的方法
//    self.customView.clipsToBounds=YES;
    self.customView.layer.masksToBounds=YES;
    
    //在view的图层上添加一个image,contents表示接受内容
    self.customView.layer.contents=(id)[UIImage imageNamed:@"me"].CGImage;
}

注意:layer中不能直接接受UI框架中的东西

   4.设置阴影

  设置阴影,不光须要设置阴影颜色,还应该设置阴影的偏移位和透明度。

  由于若是不设置偏移位的话,那么阴影和layer彻底重叠,且默认透明度为0(即彻底透明)。

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置阴影的颜色
    self.customView.layer.shadowColor=[UIColor blackColor].CGColor;
    //设置阴影的偏移量,若是为正数,则表明为往右边偏移
    self.customView.layer.shadowOffset=CGSizeMake(15, 5);
    //设置阴影的透明度(0~1之间,0表示彻底透明)
    self.customView.layer.shadowOpacity=0.6;
}

补充说明:若是设置了超过主图层的部分减掉,则设置阴影不会有显示效果。

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置边框的宽度为20
    self.customView.layer.borderWidth=5;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor blackColor].CGColor;
    
    //设置layer的圆角
    self.customView.layer.cornerRadius=20;
    //设置超过子图层的部分裁减掉
    //UI框架中使用的方法
    //    self.customView.clipsToBounds=YES;
    self.customView.layer.masksToBounds=YES;
    
    //在view的图层上添加一个image,contents表示接受内容
    self.customView.layer.contents=(id)[UIImage imageNamed:@"me"].CGImage;
    
    //设置阴影的颜色
    self.customView.layer.shadowColor=[UIColor blackColor].CGColor;
    //设置阴影的偏移量,若是为正数,则表明为往右边偏移
    self.customView.layer.shadowOffset=CGSizeMake(15, 5);
    //设置阴影的透明度(0~1之间,0表示彻底透明)
    self.customView.layer.shadowOpacity=0.6;
}

  

把剪切超出主图层部分的代码注释掉以后的显示效果

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置边框的宽度为20
    self.customView.layer.borderWidth=5;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor blackColor].CGColor;
    
    //设置layer的圆角
    self.customView.layer.cornerRadius=20;
    //设置超过子图层的部分裁减掉
    //UI框架中使用的方法
    //    self.customView.clipsToBounds=YES;
//    self.customView.layer.masksToBounds=YES;
    
    //在view的图层上添加一个image,contents表示接受内容
    self.customView.layer.contents=(id)[UIImage imageNamed:@"me"].CGImage;
    
    //设置阴影的颜色
    self.customView.layer.shadowColor=[UIColor blackColor].CGColor;
    //设置阴影的偏移量,若是为正数,则表明为往右边偏移
    self.customView.layer.shadowOffset=CGSizeMake(15, 5);
    //设置阴影的透明度(0~1之间,0表示彻底透明)
    self.customView.layer.shadowOpacity=0.6;
}

5.只要继承自uiview的都有layer属性,下面以图片为例进行说明。

在storyboard中新添加一张图片。

简单设置示例

#import "YYViewController.h"


@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;


@end


@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置图片layer的边框宽度和颜色
    self.iconView.layer.borderColor=[UIColor brownColor].CGColor;
    self.iconView.layer.borderWidth=5;
    
    //设置layer的圆角
    self.iconView.layer.cornerRadius=20;
    //设置超过子图层的部分裁减掉
       self.iconView.layer.masksToBounds=YES;
}

设置bounds属性,在设置时须要去除掉storyboard中的自动布局属性。

       

设置图片的形变属性(transform)

@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    //经过uiview设置(2D效果)
//    self.iconView.transform=CGAffineTransformMakeTranslation(0, -100);
    //经过layer来设置(3D效果,x,y,z三个方向)
    self.iconView.layer.transform=CATransform3DMakeTranslation(100, 20, 0);
}

经过uiview设置(2D效果)
       
经过layer设置(3D效果)
       
使用KVC进行设置


#import "YYViewController.h"


@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;


@end


@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    //经过uiview设置(2D效果)
//    self.iconView.transform=CGAffineTransformMakeTranslation(0, -100);
    //经过layer来设置(3D效果,x,y,z三个方向)
//    self.iconView.layer.transform=CATransform3DMakeTranslation(100, 20, 0);
    
    //经过KVC来设置
    NSValue *v=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(100, 20, 0)];
    [self.iconView.layer setValue:v forKeyPath:@"transform"];
    //若是是只须要设置在某一个方向上的移动,能够参考下面的代码
    //在x的方向上向左移动100
    [self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
}

查看苹果的官方文档,下面的属性均可以经过KVC进行设置。

旋转一个弧度

@implementation YYViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    //经过uiview设置(2D效果)
//    self.iconView.transform=CGAffineTransformMakeTranslation(0, -100);
    //经过layer来设置(3D效果,x,y,z三个方向)
//    self.iconView.layer.transform=CATransform3DMakeTranslation(100, 20, 0);
    
    //经过KVC来设置
//    NSValue *v=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(100, 20, 0)];
//    [self.iconView.layer setValue:v forKeyPath:@"transform"];
//    //若是是只须要设置在某一个方向上的移动,能够参考下面的代码
//    //在x的方向上向左移动100
//    [self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
    
    //旋转
    self.iconView.layer.transform=CATransform3DMakeRotation(M_PI_4, 1, 1, 0.5);
}

补充:三维坐标系

相关文章
相关标签/搜索