CALayer - 4

来源:互联网 发布:江西省网络作家协会 编辑:程序博客网 时间:2024/06/05 19:19

这一节来看看两个非常重要的属性:

/* The position in the superlayer that the anchor point of the layer's * bounds rect is aligned to. Defaults to the zero point. Animatable. */@property CGPoint position;

/* Defines the anchor point of the layer's bounds rect, as a point in * normalized layer coordinates - '(0, 0)' is the bottom left corner of * the bounds rect, '(1, 1)' is the top right corner. Defaults to * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */@property CGPoint anchorPoint;

看两个介绍就能大致知道意义:

position是锚点anchor point在super view 的点,而anchor point则是表示矩形中的一点,其范围是 ( 0 , 1),这个看文顶顶大神:

http://www.cnblogs.com/wendingding/p/3800736.html     的解释非常清楚,这里只讲解一些需要非常注意的地方

尤其注意,layer.position 是通过自己来指定superView的位置,而不是父层来指定子层的位置。所以position是不用提前计算的,你的subLayer添加在图层上的时候开始计算了!!这个可以自己输出看看!!


上面文顶顶大神例子我觉得还是不够贴切,这里用这个小伙伴的例子:

http://blog.163.com/it__man/blog/static/137199904201532345449/

    UIView* vview = [UIView new];    [vview setFrame:CGRectMake(0, 0, 200, 100)];    //可以试下,bounds,如果使用bounds你怎么变都木有效果的呢    //[vview setBounds:CGRectMake(0, 0, 200, 100)];    [vview setBackgroundColor:[UIColor greenColor]];        [self.view addSubview:vview];



设置anchorpoint:

    UIView* vview = [UIView new];    [vview setFrame:CGRectMake(0, 0, 200, 100)];    //可以试下,bounds,如果使用bounds你怎么变都木有效果的呢    //[vview setBounds:CGRectMake(0, 0, 200, 100)];    [vview setBackgroundColor:[UIColor greenColor]];        [self.view addSubview:vview];        [vview.layer setAnchorPoint:CGPointZero];


如果我们要恢复原来的就只要通过green layer设置superview的position:

    UIView* vview = [UIView new];    [vview setFrame:CGRectMake(0, 0, 200, 100)];    //可以试下,bounds,如果使用bounds你怎么变都木有效果的呢    //[vview setBounds:CGRectMake(0, 0, 200, 100)];    [vview setBackgroundColor:[UIColor greenColor]];        [self.view addSubview:vview];        [vview.layer setAnchorPoint:CGPointZero];            vview.layer.position = CGPointMake(0, 0);

非常好理解吧!


但是其实要彻底理解这个概念其实要结合Frame anchorPoint Position来理解。

将在下一篇详细总结。







0 0
原创粉丝点击