CALayer繪圖

#ifndef PathTest_FKContext_h

#define PathTest_FKContext_h

/*

該方法負(fù)責(zé)繪制圓角矩形

x1、y2:是圓角矩形左上角的座標(biāo)。

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

radius:控制圓角矩形的四個(gè)圓角的半徑

*/

void CGContextAddRoundRect(CGContextRef c, CGFloat x1 , CGFloat y1

, CGFloat width , CGFloat height , CGFloat radius)

{

// 移動(dòng)到左上角

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);

}

/*

該方法負(fù)責(zé)繪制多角星。

n:該參數(shù)通常應(yīng)設(shè)為奇數(shù),控制繪制N角星。

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

size:控制N角星的大小

*/

void CGContextAddStar(CGContextRef c , NSInteger n

, CGFloat dx , CGFloat dy , NSInteger size)

{

CGFloat dig = 4 * M_PI / n ;

// 移動(dòng)到指定點(diǎn)

CGContextMoveToPoint(c , dx , dy + size);

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

{

CGFloat x = sin(i * dig);

CGFloat y = cos(i * dig);

// 繪制從當(dāng)前點(diǎn)連接到指定點(diǎn)的線條

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設(shè)置圓角邊框--------------

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

self.view.layer.cornerRadius = 8;

// 設(shè)置該視圖控制器所顯示的UIView上的CALayer的邊框?qū)挾?/p>

self.view.layer.borderWidth = 4;

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

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

//--------------創(chuàng)建簡(jiǎn)單的CALayer--------------

// 創(chuàng)建一個(gè)CALayer對(duì)象

CALayer *subLayer = [CALayer layer];

// 設(shè)置subLayer的背景顏色

subLayer.backgroundColor = [UIColor magentaColor].CGColor;

// 設(shè)置subLayer的圓角半徑

subLayer.cornerRadius = 8;

// 設(shè)置subLayer的邊框?qū)挾?/p>

subLayer.borderWidth = 2;

// 設(shè)置subLayer的背景色

subLayer.borderColor = [UIColor blackColor].CGColor;

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

subLayer.shadowOffset = CGSizeMake(4, 5);

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

subLayer.shadowRadius = 1;

// 設(shè)置subLayer的陰影的顏色

subLayer.shadowColor = [UIColor blackColor].CGColor;

// 設(shè)置subLayer的陰影的透明度

subLayer.shadowOpacity = 0.8;

// 設(shè)置subLayer的大小和位置

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

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

[self.view.layer addSublayer:subLayer];

// 再創(chuàng)建一個(gè)CALayer對(duì)象

CALayer *subLayer2 = [CALayer layer];

// 設(shè)置該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顯示圖片--------------

// 再創(chuàng)建一個(gè)CALayer對(duì)象

CALayer *imageLayer = [CALayer layer];

// 設(shè)置該imageLayer顯示的圖片

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

// 設(shè)置iamgeLayer的大小和位置。

imageLayer.frame = subLayer2.bounds;

// 將imageLayer添加到subLayer2中

[subLayer2 addSublayer:imageLayer];

//--------------自定義CALayer的繪制內(nèi)容--------------

// 再創(chuàng)建一個(gè)CALayer對(duì)象

CALayer *customDrawn = [CALayer layer];

// 設(shè)置CALayer的委托對(duì)象,該委托對(duì)象負(fù)責(zé)該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];

// 必須有這行,這行才會(huì)通知CALayer調(diào)用delegate的drawLayer方法

[customDrawn setNeedsDisplay];

[super viewDidLoad];

}

// 重寫該方法為CALayer繪制自定義內(nèi)容

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

UIColor* bgColor = [UIColor colorWithPatternImage:

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

// 設(shè)置繪圖的背景色

CGContextSetFillColorWithColor(context, bgColor.CGColor);

// 填充一個(gè)橢圓

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

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