天天看點

CALayer - 4

這一節來看看兩個非常重要的屬性:

/* 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];
           
CALayer - 4

設定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];
           
CALayer - 4

如果我們要恢複原來的就隻要通過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來了解。

将在下一篇詳細總結。