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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

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