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阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,321评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,001评论 5 13
  • 转载:http://www.jianshu.com/p/32fcadd12108 每个UIView有一个伙伴称为l...
    F麦子阅读 5,958评论 0 13
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 3,538评论 7 11
  • CALayer 当利用drawRect:方法绘图的本质就是绘制到了UIView的layer(属性)中。 CALay...
    HWenj阅读 1,297评论 2 5