1、和視圖一樣叁执,圖層在"圖層樹(shù)"當(dāng)中也是相對(duì)于父圖層按層級(jí)關(guān)系放置茄厘,一個(gè)圖層的position依賴(lài)于它父圖層的bounds矮冬,如果父圖層發(fā)生了移動(dòng)谈宛,它的所有子圖層也會(huì)跟著移動(dòng)
2、有時(shí)候你需要知道一個(gè)圖層的絕對(duì)位置胎署,或者是相對(duì)于另一個(gè)圖層的位置吆录,而不是它當(dāng)前父圖層的位置,CALayer給不同坐標(biāo)系之間的圖層轉(zhuǎn)換提供了一些工具類(lèi)方法琼牧,這些方法可以把定義在一個(gè)圖層坐標(biāo)系下的點(diǎn)或者矩形轉(zhuǎn)換成另一個(gè)圖層坐標(biāo)系下的點(diǎn)或者矩形
- (CGPoint)convertPoint:(CGPoint)point fromLayer:(CALayer *)layer;
- (CGPoint)convertPoint:(CGPoint)point toLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect fromLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect toLayer:(CALayer *)layer;
2恢筝、翻轉(zhuǎn)的幾何結(jié)構(gòu)
- 1、常規(guī)來(lái)說(shuō)巨坊,在iOS上撬槽,一個(gè)圖層的position位于父圖層的左上角,但是在Mac OS上趾撵,通常是位于在做下角吭服。Core Animation可以通過(guò)geometryFlipped屬性來(lái)適配這兩種情況兵拢,它決定了一個(gè)圖層的坐標(biāo)是否相對(duì)于父圖層垂直翻轉(zhuǎn),是一個(gè)Bool類(lèi)型。
3友鼻、和UIView嚴(yán)格的二維坐標(biāo)系不同,CALayer存在于一個(gè)三維空間當(dāng)中婴氮。除了position和anchorPoint屬性之外竿裂,CALayer還有另外兩個(gè)屬性,zposition和anchorPointZ剿涮,二者都是在Z軸上描述圖層位置的浮點(diǎn)類(lèi)型言津。
4、圖層是根據(jù)它們子圖層的sublayers出現(xiàn)的順序來(lái)繪制的取试,這就是所謂的畫(huà)家的算法--就像一個(gè)畫(huà)家在墻上作畫(huà)悬槽,后被繪制上的圖層將會(huì)遮蓋住之前的圖層,但是通過(guò)設(shè)置圖層的zPosition想括,來(lái)改變圖層的層級(jí)關(guān)系
#import "ViewController.h"
@interface ViewController ()
/**
* 綠色View
*/
@property (nonatomic, strong) UIView *greenView;
/**
* 紅色View
*/
@property (nonatomic, strong) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.greenView];
self.redView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 200, 200)];
self.redView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.redView];
}
@end
5陷谱、我們希望在真實(shí)的應(yīng)用中也能顯示出繪圖的順序,同樣地,如果我們提高綠色視圖的zPosition烟逊,我們會(huì)發(fā)現(xiàn)順序就反了渣窜。其實(shí)并不需要增加太多,視圖都非常地薄宪躯,所以給zPosition提高一個(gè)像素就可以讓綠色視圖前置乔宿,當(dāng)然0.1或者0.0001也能夠做到,但是最好不要這樣访雪,因?yàn)楦↑c(diǎn)類(lèi)型四舍五入的計(jì)算可能造成一些不便的麻煩
#import "ViewController.h"
@interface ViewController ()
/**
* 綠色View
*/
@property (nonatomic, strong) UIView *greenView;
/**
* 紅色View
*/
@property (nonatomic, strong) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.greenView];
self.redView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 200, 200)];
self.redView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.redView];
// 改變 綠色View的圖層zPosition
self.greenView.layer.zPosition = 1.0;
// // 改變 紅色View的圖層zPosition
// self.redView.layer.zPosition = 1.0;
}
@end
效果如下:
小結(jié)
- 1详瑞、當(dāng)圖層的zPosition一樣的時(shí)候,顯示的層級(jí)順序和View添加到視圖的順序相反
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者