简单理解CALayer的position和anchorPoint

position

简单可以理解为layer的中心点,类似于UIView的center。比如设置一个layer的frame为(0,0,100,100),那么它的position默认为(50,50),如果单独设置为layer.positon = CGPointMake(100,100),那么它的fram就会变为(50,50,100,100);

anchorPoint

锚点:图层中和position重合的点,锚点是按照X Y的比例来表示,坐标原点即为(0,0),图层中心点即为(0.5,0.5),图层右下角即为 (1,1);
定义锚点的值就意味着图层上这个点会带着图层移动到position的位置。

以下是一个简单的示意图

 self.aLayer = [CALayer layer];
    self.aLayer.frame = CGRectMake(0, 0, 100, 100);
    
//    self.aLayer.position = CGPointMake(100, 100); //位置
    self.aLayer.backgroundColor = [UIColor redColor].CGColor;
    self.aLayer.opacity = 0.3;//透明度
//    self.aLayer.anchorPoint = CGPointMake(0, 0); //锚点
    [self.view.layer addSublayer:self.aLayer];
默认.png

这里仅仅设置了frame(因为layer的frame不支持隐式动画,所以一般情况都是使用position和bounds来进行设置),那么据图可知,中心点即position为(50,50),锚点也是默认是(50,50)

   self.aLayer = [CALayer layer];
    self.aLayer.frame = CGRectMake(0, 0, 100, 100);
    
    self.aLayer.position = CGPointMake(100, 100); //中心点
    self.aLayer.backgroundColor = [UIColor redColor].CGColor;
    self.aLayer.opacity = 0.3;//透明度
//    self.aLayer.anchorPoint = CGPointMake(0, 0); //锚点
    [self.view.layer addSublayer:self.aLayer];
![Upload anchorPoint.gif failed. Please try again.]

单独设置position后可以发现,图层中心点为(100,100)了,相应的frame变为(50,50,100,100);

接下来以position = (100,100)为前提,设置了5组锚点的值进行简单了解

- (void)creatButton
{
    NSArray *dataArray = @[@"{0 ,0}",@"{0,0.5}",@"{0.5,0.5}",@"{1,1}",@"{0.5,1}"];
    CGFloat spacing = 10;
    CGFloat width = (SCRREN_WIDTH - (dataArray.count + 1) * spacing) / dataArray.count;
    CGFloat height = 50;
    for (int i = 0; i<dataArray.count; i++) {
        UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame = CGRectMake(spacing + i * (width + spacing) , 300, width, height);
        changeBtn.tag = 100 + i;
        changeBtn.layer.cornerRadius = 5;
        changeBtn.backgroundColor = [UIColor blueColor];
        [changeBtn setTitle:dataArray[i] forState:UIControlStateNormal];
        [changeBtn addTarget:self action:@selector(changeAnchorPoint:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:changeBtn];
    }
}

- (void)changeAnchorPoint:(UIButton *)sender
{
    [UIView animateWithDuration:1 animations:^{
        self.aLayer.anchorPoint = CGPointFromString(sender.titleLabel.text);
    }];
}
anchorPoint.gif

layer ->frame.png

通过图片可以发现,定义的锚点带着图层移动到了position的位置,其相应的frame也跟着发生变化。
以上就是一些简单的理解,因为老是会忘记,所以特地记录一下,也方便有地方可以进行查看。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,557评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做Laye...
    小猫仔阅读 3,786评论 1 4
  • 本文转载自:http://www.cocoachina.com/ios/20150104/10814.html ...
    idiot_lin阅读 1,595评论 0 11
  • 12年3月24日,我们俩仓促间决定在一起,更确切的说应该是我还没有准备好就在一起了,短短半年多恋情后随之而来的就是...
    苏芬阅读 245评论 0 0