CALayer使用

每个uiview都有一个层,每个层可以有多个子层。

1)layer的设计目的不是取代视图,不能基于layer创建一个可视化的控件

2)layer设计目的是提供视图的基本可视内容。提高动画的执行效率。主要是提高动画的执行效率。

3)除了提供可视内容。layer不负责事件响应,内容填充,layer不参与到响应者链条中。

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CALayer *layer = [CALayer layer];
    [self.view.layer addSublayer:layer];
    
    // 设置边界
    [layer setBounds:CGRectMake(0, 0, 200, 200)];
    // 设置背景颜色
    [layer setBackgroundColor:[UIColor redColor].CGColor];
    // 设置中心点,如果没有设置,默认是图形的中心点
    [layer setPosition:CGPointMake(100, 100)];
    // 设置内容
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    [layer setContents:(id)image.CGImage];
    // 设置锚点AnchorPoint这个是定位点,在0到1之间。默认是0.5,0.5,表示图形的中心点。
    // 如果是1,1表示右下角点。如果是0,0表示左上角点。图层的定位就是根据Bounds,position
    // ,anchorpoint这几个属性来确定的。
    [layer setAnchorPoint:CGPointMake(0, 0)];
// 在这个时候的结果是


// 将anchorpoint改成1,1

将anchorpoint改成0.5,0.5


}

 锚点主要是用来控制图形的位置以及图像旋转的轴

图层旋转就是根据图层的锚点来旋转的。position和anchorpoint可以确定图层相对于position的位置。

对于图层来说,把用图层的bounds的长和宽乘以锚点,把得出的点,移动到positioin.根据这个点,把整个图层移动过去。

锚点是相对于图层的位置。position是相对于父视图的位置。把锚点对应的点,移动到position,这样把整个视图移动过去。

 

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CALayer *layer = [CALayer layer];
    [self.view.layer addSublayer:layer];
    
    // 设置边界
    [layer setBounds:CGRectMake(0, 0, 200, 200)];
    // 设置背景颜色
    [layer setBackgroundColor:[UIColor redColor].CGColor];
    // 设置中心点,如果没有设置,默认是图形的中心点
    [layer setPosition:CGPointMake(0, 0)];
    // 设置内容
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    [layer setContents:(id)image.CGImage];
    // 设置锚点AnchorPoint这个是定位点,在0到1之间。默认是0.5,0.5,表示图形的中心点。
    // 如果是1,1表示右下角点。如果是0,0表示左上角点。图层的定位就是根据Bounds,position
    // ,anchorpoint这几个属性来确定的。
    [layer setAnchorPoint:CGPointMake(0, 0)];
    self.mylayer = layer;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 这个动作有动画效果。有瘾式动画,这个动画执行的时间是0.25秒。是图层的隐式动画
if (self.mylayer.anchorPoint.x == 0) { self.mylayer.anchorPoint = CGPointMake(1, 1); } else { self.mylayer.anchorPoint = CGPointMake(0, 0); } }

 在做游戏的时候,也是大量使用了锚点。根据锚点来转动。利用锚点移动图层,不用知道视图大小只要知道position和anchorpoint就行。

 POSITION是0,锚点也是0的时候旋转45度,得到结果

代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CALayer *layer = [CALayer layer];
    [self.view.layer addSublayer:layer];
    
    // 设置边界
    [layer setBounds:CGRectMake(0, 0, 200, 200)];
    // 设置背景颜色
    [layer setBackgroundColor:[UIColor redColor].CGColor];
    // 设置中心点,如果没有设置,默认是图形的中心点
    [layer setPosition:CGPointMake(0, 0)];
    // 设置内容
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    [layer setContents:(id)image.CGImage];
    // 设置锚点AnchorPoint这个是定位点,在0到1之间。默认是0.5,0.5,表示图形的中心点。
    // 如果是1,1表示右下角点。如果是0,0表示左上角点。图层的定位就是根据Bounds,position
    // ,anchorpoint这几个属性来确定的。
    [layer setAnchorPoint:CGPointMake(0, 0)];
    self.mylayer = layer;
    // 旋转模式是顺时针方向。
    [self.mylayer setTransform:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];

     // 使用keypath方式设置图层旋转


    [self.mylayersetValue:@(M_PI_4)forKeyPath:@"transform.rotation.z"];


}
结果

如果anchorpoint设置成了0.5,0.5的话,再旋转45度。就回变成

如果把旋转取消,显示的效果是

锚点的作用就是
1)定位图层的位置
2)控制旋转,作为旋转的轴点。
锚点最常用的需求
游戏里人物的摆臂动作。把锚点定位在胳膊的上端。

 

 

 

 

 

 

 

 

posted on 2013-10-01 16:28  老猫zl  阅读(385)  评论(0编辑  收藏  举报