CALayer繪圖

#ifndef PathTest_FKContext_h

#define PathTest_FKContext_h

/*

該方法負責繪制圓角矩形

x1、y2:是圓角矩形左上角的座標。

width、height:控制圓角舉行的寬、高

radius:控制圓角矩形的四個圓角的半徑

*/

void CGContextAddRoundRect(CGContextRef c, CGFloat x1 , CGFloat y1

, CGFloat width , CGFloat height , CGFloat radius)

{

// 移動到左上角

CGContextMoveToPoint (c, x1 + radius , y1);

// 添加一條連接到右上角的線段

CGContextAddLineToPoint(c , x1 + width - radius, y1);

// 添加一段圓弧

CGContextAddArcToPoint(c , x1 + width , y1, x1 + width

, y1 + radius, radius);

// 添加一條連接到右下角的線段

CGContextAddLineToPoint(c , x1 + width, y1 + height - radius);

// 添加一段圓弧

CGContextAddArcToPoint(c , x1 + width, y1 + height

, x1 + width - radius , y1 + height , radius);

// 添加一條連接到左下角的線段

CGContextAddLineToPoint(c , x1 + radius, y1 + height);

// 添加一段圓弧

CGContextAddArcToPoint(c , x1, y1 + height , x1

, y1 + height - radius , radius);

// 添加一條連接到左上角的線段

CGContextAddLineToPoint(c , x1 , y1 + radius);

// 添加一段圓弧

CGContextAddArcToPoint(c , x1 , y1 , x1 + radius , y1 , radius);

}

/*

該方法負責繪制多角星。

n:該參數通常應設為奇數,控制繪制N角星。

dx、dy:控制N角星的中心。

size:控制N角星的大小

*/

void CGContextAddStar(CGContextRef c , NSInteger n

, CGFloat dx , CGFloat dy , NSInteger size)

{

CGFloat dig = 4 * M_PI / n ;

// 移動到指定點

CGContextMoveToPoint(c , dx , dy + size);

for(int i = 1 ; i <= n ; i++)

{

CGFloat x = sin(i * dig);

CGFloat y = cos(i * dig);

// 繪制從當前點連接到指定點的線條

CGContextAddLineToPoint(c , x * size + dx ,y * size + dy);

}

}

#endif



#import "FKViewController.h"

#import "FKContext.h"

@implementation FKViewController

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor grayColor];

//--------------為UIView設置圓角邊框--------------

// 設置該視圖控制器所顯示的UIView上的CALayer的圓角半徑

self.view.layer.cornerRadius = 8;

// 設置該視圖控制器所顯示的UIView上的CALayer的邊框寬度

self.view.layer.borderWidth = 4;

// 設置該視圖控制器所顯示的UIView上的CALayer的邊框顏色

self.view.layer.borderColor = [UIColor redColor].CGColor;

//--------------創建簡單的CALayer--------------

// 創建一個CALayer對象

CALayer *subLayer = [CALayer layer];

// 設置subLayer的背景顏色

subLayer.backgroundColor = [UIColor magentaColor].CGColor;

// 設置subLayer的圓角半徑

subLayer.cornerRadius = 8;

// 設置subLayer的邊框寬度

subLayer.borderWidth = 2;

// 設置subLayer的背景色

subLayer.borderColor = [UIColor blackColor].CGColor;

// 設置subLayer的陰影偏移(此處向右下角投下陰影)

subLayer.shadowOffset = CGSizeMake(4, 5);

// 設置subLayer的陰影的模糊程度(該屬性值越大,陰影越模糊)

subLayer.shadowRadius = 1;

// 設置subLayer的陰影的顏色

subLayer.shadowColor = [UIColor blackColor].CGColor;

// 設置subLayer的陰影的透明度

subLayer.shadowOpacity = 0.8;

// 設置subLayer的大小和位置

subLayer.frame = CGRectMake(30, 30, 120, 160);

// 將subLayer添加到該視圖控制器所顯示的UIView上的CALayer

[self.view.layer addSublayer:subLayer];

// 再創建一個CALayer對象

CALayer *subLayer2 = [CALayer layer];

// 設置該CALayer的邊框、陰影、大小、位置等屬性

subLayer2.cornerRadius = 8;

subLayer2.borderWidth = 2;

subLayer2.borderColor = [UIColor blackColor].CGColor;

subLayer2.shadowOffset = CGSizeMake(4, 5);

subLayer2.shadowRadius = 1;

subLayer2.shadowColor = [UIColor blackColor].CGColor;

subLayer2.shadowOpacity = 0.8;

subLayer2.masksToBounds = YES;

subLayer2.frame = CGRectMake(170, 30, 120, 160);

// 將subLayer2添加到該視圖控制器所顯示的UIView上的CALayer

[self.view.layer addSublayer:subLayer2];

//-------------使用CALayer顯示圖片--------------

// 再創建一個CALayer對象

CALayer *imageLayer = [CALayer layer];

// 設置該imageLayer顯示的圖片

imageLayer.contents = (id)[[UIImage imageNamed:@"android"] CGImage];

// 設置iamgeLayer的大小和位置。

imageLayer.frame = subLayer2.bounds;

// 將imageLayer添加到subLayer2中

[subLayer2 addSublayer:imageLayer];

//--------------自定義CALayer的繪制內容--------------

// 再創建一個CALayer對象

CALayer *customDrawn = [CALayer layer];

// 設置CALayer的委托對象,該委托對象負責該CALayer的繪制

customDrawn.delegate = self;

customDrawn.backgroundColor = [UIColor greenColor].CGColor;

customDrawn.frame = CGRectMake(30, 220, 260, 210);

customDrawn.shadowOffset = CGSizeMake(0, 3);

customDrawn.shadowRadius = 5.0;

customDrawn.shadowColor = [UIColor blackColor].CGColor;

customDrawn.shadowOpacity = 0.8;

customDrawn.cornerRadius = 10.0;

customDrawn.borderColor = [UIColor blackColor].CGColor;

customDrawn.borderWidth = 2.0;

customDrawn.masksToBounds = YES;

[self.view.layer addSublayer:customDrawn];

// 必須有這行,這行才會通知CALayer調用delegate的drawLayer方法

[customDrawn setNeedsDisplay];

[super viewDidLoad];

}

// 重寫該方法為CALayer繪制自定義內容

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {

UIColor* bgColor = [UIColor colorWithPatternImage:

[UIImage imageNamed:@"heart.gif"]];

// 設置繪圖的背景色

CGContextSetFillColorWithColor(context, bgColor.CGColor);

// 填充一個橢圓

CGContextFillEllipseInRect(context, CGRectMake(20 , 20 , 100 , 100));

CGContextAddRoundRect(context, 160, 20, 100, 60, 5);

CGContextFillPath(context);

CGContextSetRGBStrokeColor(context, 1, 1, 0, 1);

CGContextStrokePath(context);

CGContextAddStar(context, 5, 140, 150, 60);

CGContextSetRGBFillColor(context, .5, 0.5, 1, 1);

CGContextFillPath(context);

for (int i = 0; i < 10; i++) {

CGContextBeginPath(context);

CGContextAddArc(context, i * 25, i * 25, 8 * (i + 1), M_PI * 1.5, M_PI, 0);

CGContextClosePath(context);

CGContextSetRGBFillColor(context, 1, 0, 1, (10 -i)* 0.1 );

CGContextFillPath(context);

}

}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,316評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,481評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,241評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,939評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,697評論 6 409
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,182評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,247評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,406評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,933評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,772評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,973評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,516評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,638評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,866評論 1 285
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,644評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,953評論 2 373

推薦閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,541評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,130評論 5 13
  • 轉載:http://www.lxweimin.com/p/32fcadd12108 每個UIView有一個伙伴稱為l...
    F麥子閱讀 6,257評論 0 13
  • 前言 本文只要描述了iOS中的Core Animation(核心動畫:隱式動畫、顯示動畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,645評論 7 11
  • CALayer 當利用drawRect:方法繪圖的本質就是繪制到了UIView的layer(屬性)中。 CALay...
    HWenj閱讀 1,362評論 2 5