CALayer可以完成很多任務(wù)
CALayer與UIView
CALayer在系統(tǒng)架構(gòu)中的位置
適用于iOS與Mac OS X
適用于iOS與Mac OS X
只適用于iOS
frame屬性
border屬性
cornerRadius(CGFloat),圓角屬性,若控件的寬高相等,且圓角屬性為寬/高的一半,則為圓形,原理如圖
borderWidth(CGFloat),邊框的寬度
,與contentInsets的效果相似
appearance屬性
content屬性
backgroundColor、content與border的層次關(guān)系,如圖
示例
若position為(100, 100),anchorPoint為(0, 0 ),如圖
若position為(100, 100),anchorPoint為(0.5, 0.5),如圖
創(chuàng)建一個CALayer對象imageLayer,并添加到控制器View的layer上
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
設(shè)置背景圖片
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//設(shè)置背景顏色imageLayer.backgroundColor = [UIColor orangeColor].CGColor;//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
顯示效果如圖
設(shè)置imageLayer的內(nèi)容為一張圖片
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//在圖層上添加一張圖片imageLayer.contents = (id)[UIImage imageNamed:@"Ali"].CGImage;//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
顯示效果如圖
設(shè)置圓角屬性
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//設(shè)置圓角屬性imageLayer.cornerRadius = 20;imageLayer.masksToBounds = YES; //該屬性在iOS9中不用設(shè)置//在圖層上添加一張圖片imageLayer.contents = (id)[UIImage imageNamed:@"Ali"].CGImage;//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
顯示效果如圖
設(shè)置邊框?qū)傩?/p>
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//設(shè)置圓角屬性imageLayer.cornerRadius = 20;imageLayer.masksToBounds = YES;//設(shè)置border屬性imageLayer.borderWidth = 2;imageLayer.borderColor = [UIColor purpleColor].CGColor;//在圖層上添加一張圖片imageLayer.contents = (id)[UIImage imageNamed:@"Ali"].CGImage;//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
顯示效果如圖
方法一:創(chuàng)建CALayer對象,并設(shè)置其代理
創(chuàng)建CALayer對象
//創(chuàng)建圖層CALayer *imageLayer = [[CALayer alloc] init];//設(shè)置代理imageLayer.delegate = self;//設(shè)置圖層的位置和尺寸imageLayer.bounds = CGRectMake(0, 0, 100, 100);imageLayer.position = CGPointMake(100, 200);//重繪[imageLayer setNeedsDisplay];//將圖層添加到控制器view的layer上[self.view.layer addSublayer:imageLayer];
實現(xiàn)代理方法,在該方法中繪制圖層的內(nèi)容
- (void)drawLayer:(nonnull CALayer *)layer inContext:(nonnull CGContextRef)ctx{ //通過繪圖方法繪制內(nèi)容}
方法二:自定義CALayer類的子類
自定義CALayer的子類ImageLayer
//重寫該方法,繪制圖層的內(nèi)容- (void)drawInContext:(nonnull CGContextRef)ctx{ //通過繪圖方法繪制內(nèi)容}
創(chuàng)建ImageLayer的實例
ImageLayer *imageLayer = [[ImageLayer alloc] init];[self.view.layer addSublayer:imageLayer];
新聞熱點
疑難解答