iOS開發UI篇—CAlayer(自定義layer)


iOS開發UI篇—CAlayer(自定義layer)

一、第一種方式

1.簡單說明

以前想要在view中畫東西,需要自定義view,創建一個類與之關聯,讓這個類繼承自UIView,然后重寫它的DrawRect:方法,然后在該方法中畫圖。

繪制圖形的步驟:
(1)獲取上下文
(2)繪制圖形
(3)渲染圖形
 
如果在layer上畫東西,與上面的過程類似。
代碼示例:
新建一個類,讓該類繼承自CALayer
YYMylayer.m文件
 1 //
 2 //  YYMylayer.m
 3 //  05-自定義layer(1)
 4 //
 5 //  Created by apple on 14-6-21.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYMylayer.h"
10 
11 @implementation YYMylayer
12 //重寫該方法,在該方法內繪制圖形
13 -(void)drawInContext:(CGContextRef)ctx
14 {
15     //1.繪制圖形
16     //畫一個圓
17     CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
18     //設置屬性(顏色)
19 //    [[UIColor yellowColor]set];
20     CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
21     
22     //2.渲染
23     CGContextFillPath(ctx);
24 }
25 @end
在控制器中,創建一個自定義的類
 1 //
 2 //  YYViewController.m
 3 //  05-自定義layer(1)
 4 //
 5 //  Created by apple on 14-6-21.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYViewController.h"
10 #import "YYMylayer.h"
11 
12 @interface YYViewController ()
13 
14 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22     //1.創建自定義的layer
23     YYMylayer *layer=[YYMylayer layer];
24     //2.設置layer的屬性
25     layer.backgroundColor=[UIColor brownColor].CGColor;
26     layer.bounds=CGRectMake(0, 0, 200, 150);
27     layer.anchorPoint=CGPointZero;
28     layer.position=CGPointMake(100, 100);
29     layer.cornerRadius=20;
30     layer.shadowColor=[UIColor blackColor].CGColor;
31     layer.shadowOffset=CGSizeMake(10, 20);
32     layer.shadowOpacity=0.6;
33     
34     [layer setNeedsDisplay];
35     //3.添加layer
36     [self.view.layer addSublayer:layer];
37     
38 }
39 
40 @end
注意點:
(1)默認為無色,不會顯示。要想讓繪制的圖形顯示出來,還需要設置圖形的顏色。注意不能直接使用UI框架中的類
(2)在自定義layer中的-(void)drawInContext:方法不會自己調用,只能自己通過setNeedDisplay方法調用,在view中畫東西DrawRect:方法在view第一次顯示的時候會自動調用。
實現效果:
2.拓展
  UIView中繪圖說明
 1 #import "YYVIEW.h"
 2 
 3 @implementation YYVIEW
 4 
 5 
 6 - (void)drawRect:(CGRect)rect
 7 {
 8     //1.獲取上下文
 9     CGContextRef ctx=UIGraphicsGetCurrentContext();
10     //2.繪制圖形
11     CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
12     //設置屬性(顏色)
13     //    [[UIColor yellowColor]set];
14     CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
15     
16     //3.渲染
17     CGContextFillPath(ctx);
18     //在執行渲染操作的時候,本質上它的內部相當於調用了下面的方法
19     [self.layer drawInContext:ctx];
20 }

說明:在UIView中繪制圖形,獲取的上下文就是這個view對應的layer的上下文。在渲染的時候,就是把圖形渲染到對應的layer上。

  在執行渲染操作的時候,本質上它的內部相當於執行了 [self.layer drawInContext:ctx];

 

二、第二種方式

方法描述:設置CALayer的delegate,然后讓delegate實現drawLayer:inContext:方法,當CALayer需要繪圖時,會調用delegate的drawLayer:inContext:方法進行繪圖。

代碼示例:

 1 //
 2 //  YYViewController.m
 3 //  06-自定義layer(2)
 4 //
 5 //  Created by apple on 14-6-21.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 
 8 #import "YYViewController.h"
 9 @interface YYViewController ()
10 @end
11 
12 @implementation YYViewController
13 
14 - (void)viewDidLoad
15 {
16     [super viewDidLoad];
17     //1.創建自定義的layer
18     CALayer *layer=[CALayer layer];
19     //2.設置layer的屬性
20     layer.backgroundColor=[UIColor brownColor].CGColor;
21     layer.bounds=CGRectMake(0, 0, 200, 150);
22     layer.anchorPoint=CGPointZero;
23     layer.position=CGPointMake(100, 100);
24     layer.cornerRadius=20;
25     layer.shadowColor=[UIColor blackColor].CGColor;
26     layer.shadowOffset=CGSizeMake(10, 20);
27     layer.shadowOpacity=0.6;
28     
29     //設置代理
30     layer.delegate=self;
31     [layer setNeedsDisplay];
32     //3.添加layer
33     [self.view.layer addSublayer:layer];
34 }
35 
36 -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
37 {
38     //1.繪制圖形
39     //畫一個圓
40     CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
41     //設置屬性(顏色)
42     //    [[UIColor yellowColor]set];
43     CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
44     
45     //2.渲染
46     CGContextFillPath(ctx);
47 }
48 @end

實現效果:

注意點:不能再將某個UIView設置為CALayer的delegate,因為UIView對象已經是它內部根層的delegate,再次設置為其他層的delegate就會出問題。

在設置代理的時候,它並不要求我們遵守協議,說明這個方法是nsobject中的,就不需要再額外的顯示遵守協議了。
提示:以后如果要設置某個類的代理,但是這個代理沒要求我們遵守什么特定的協議,那么可以認為這個協議方法是NSObject里邊的。
 
三、補充說明
(1)無論采取哪種方法來自定義層,都必須調用CALayer的setNeedsDisplay方法才能正常繪圖。
(2)詳細現實過程:
當UIView需要顯示時,它內部的層會准備好一個CGContextRef(圖形上下文),然后調用delegate(這里就是UIView)的drawLayer:inContext:方法,並且傳入已經准備好的CGContextRef對象。而UIView在drawLayer:inContext:方法中又會調用自己的drawRect:方法。平時在drawRect:中通過UIGraphicsGetCurrentContext()獲取的就是由層傳入的CGContextRef對象,在drawRect:中完成的所有繪圖都會填入層的CGContextRef中,然后被拷貝至屏幕。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM