iOS绘图——Quartz 2D使用方法

自定义UIView时,只需要重写drawRect:方法
使用Quartz 2D其实也很简单
首先必须要获取绘图的环境,即获取上下文CGContextRef
然后使用各种API进行绘图就可以了(这里有很多API,只有用得多了才能记住)
接下来通过绘制不同的图形来做详细讲解(具体解释都在注释中)
1、绘制几何图形

- (void)drawRect:(CGRect)rect
{
// 获取绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置线宽
CGContextSetLineWidth(ctx, 16);
CGContextSetRGBStrokeColor(ctx, 0 , 1, 0 , 1);
// ----------下面绘制3个线段测试端点形状-----------
// 定义4个点,绘制线段
const CGPoint points1[] =  {CGPointMake(10 , 20), CGPointMake(90 , 20)
,CGPointMake(90 , 20) , CGPointMake(20, 50)};

// 绘制线段(默认不绘制端点)
CGContextStrokeLineSegments(ctx ,points1 , 4);

// 设置线段的端点形状:方形端点
CGContextSetLineCap(ctx, kCGLineCapSquare);
// 定义4个点,绘制线段
const CGPoint points2[] =  {CGPointMake(110 , 20), CGPointMake(190 , 20)
,CGPointMake(190 , 20) , CGPointMake(120, 50)};

// 绘制线段
CGContextStrokeLineSegments(ctx ,points2 , 4);
// 设置线段的端点形状:圆形端点
CGContextSetLineCap(ctx, kCGLineCapRound);
// 定义4个点,绘制线段
const CGPoint points3[] =  {CGPointMake(210 , 20), CGPointMake(300 , 20)
,CGPointMake(300 , 20) , CGPointMake(220, 50)};

// 绘制线段
CGContextStrokeLineSegments(ctx ,points3 , 4);
// ----------下面绘制3个线段测试点线模式-----------
// 设置线段的端点形状
CGContextSetLineCap(ctx, kCGLineCapButt);
// 设置线宽
CGContextSetLineWidth(ctx, 10);
CGFloat patterns1[] = {6 , 10};
// 设置点线模式:实线宽6,间距宽10
CGContextSetLineDash(ctx , 0 , patterns1 , 1);
// 定义2个点,绘制线段
const CGPoint points4[] = {CGPointMake(40 , 65), CGPointMake(280 , 65)};
// 绘制线段
CGContextStrokeLineSegments(ctx ,points4 , 2);
// 设置点线模式:实线宽6,间距宽10,但第一个实线宽为3
CGContextSetLineDash(ctx , 3 , patterns1 , 1);
// 定义2个点,绘制线段
const CGPoint points5[] = {CGPointMake(40 , 85), CGPointMake(280 , 85)};
// 绘制线段
CGContextStrokeLineSegments(ctx ,points5 , 2);
CGFloat patterns2[] = {5,1,4};//代表长为5的实线、距离为3的间距、长为4的实线、距离为5的间距.....这种点线模式
// 设置点线模式
CGContextSetLineDash(ctx , 0 , patterns2 , 3);
const CGPoint points6[] = {CGPointMake(40 , 105), CGPointMake(280 , 105)};
// 绘制线段
CGContextStrokeLineSegments(ctx ,points6 , 2);
// ---------下面填充矩形---------
// 设置线条颜色
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
// 设置线条宽度
CGContextSetLineWidth(ctx, 14);
// 设置填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
// 填充一个矩形
CGContextFillRect(ctx , CGRectMake(30 , 120 , 120 , 60));
// 设置填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor yellowColor].CGColor);
// 填充一个矩形
CGContextFillRect(ctx , CGRectMake(80 , 160 , 120 , 60));
// ---------下面绘制矩形边框---------
// 取消设置点线模式
CGContextSetLineDash(ctx, 0, 0, 0);
// 绘制一个矩形边框
CGContextStrokeRect(ctx , CGRectMake(30 , 230 , 120 , 60));
// 设置线条颜色
CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);
// 设置线条连接点的形状
CGContextSetLineJoin(ctx, kCGLineJoinRound);
// 绘制一个矩形边框
CGContextStrokeRect(ctx , CGRectMake(80 , 260 , 120 , 60));
// 设置线条颜色
CGContextSetRGBStrokeColor(ctx, 1.0, 0, 1.0 , 1.0);
// 设置线条连接点的形状
CGContextSetLineJoin(ctx, kCGLineJoinBevel);
// 绘制一个矩形边框
CGContextStrokeRect(ctx , CGRectMake(130 , 290 , 120 , 60));
// 设置线条颜色
CGContextSetRGBStrokeColor(ctx, 0, 1 , 1 , 1);
// ---------下面绘制和填充一个椭圆---------
// 绘制一个椭圆
CGContextStrokeEllipseInRect(ctx , CGRectMake(30 , 380 , 120 , 60));
// 设置填充颜色
CGContextSetRGBFillColor(ctx, 1, 0 , 1 , 1);
// 填充一个椭圆
CGContextFillEllipseInRect(ctx , CGRectMake(180 , 380 , 120 , 60));
}

2、绘制文本

- (void)drawRect:(CGRect)rect
{
// 获取该控件的绘图CGContextRef
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置字符间距
CGContextSetCharacterSpacing (ctx, 4);
// 设置填充颜色
CGContextSetRGBFillColor (ctx, 1, 0, 1, 1);
// 设置线条颜色
CGContextSetRGBStrokeColor (ctx, 0, 0, 1, 1);
// 设置使用填充模式绘制文字
CGContextSetTextDrawingMode (ctx, kCGTextFill);
// 绘制文字

[@"我爱iOS" drawAtPoint:CGPointMake(10 ,20)
withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Arial Rounded MT Bold" size: 45],


NSFontAttributeName,

[UIColor magentaColor] , NSForegroundColorAttributeName , nil]];

// 设置使用描边模式绘制文字
CGContextSetTextDrawingMode (ctx, kCGTextStroke);
// 绘制文字
[@"我爱学习" drawAtPoint:CGPointMake(10 ,80)
withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Heiti SC" size: 40],NSFontAttributeName,

[UIColor blueColor] , NSForegroundColorAttributeName , nil]];

// 设置使用填充、描边模式绘制文字
CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
// 绘制文字
[@"学习不爱我" drawAtPoint:CGPointMake(10 ,130)
withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Heiti SC" size: 50], NSFontAttributeName,

[UIColor magentaColor] , NSForegroundColorAttributeName , nil]];

// 定义一个垂直镜像的变换矩阵
CGAffineTransform yRevert = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
// 设置绘制文本的字体和字体大小
CGContextSelectFont (ctx, "Courier New" , 40
,kCGEncodingMacRoman);

// 为yRevert变换矩阵根据scaleRate添加缩放变换矩阵
CGAffineTransform scale = CGAffineTransformScale(yRevert,
self.scaleRate, self.scaleRate);

// 为scale变换矩阵根据rotateAngle添加旋转变换矩阵

CGAffineTransform rotate = CGAffineTransformRotate(scale
, M_PI * self.rotateAngle / 180);

// 对CGContextRef绘制文字时应用变换
CGContextSetTextMatrix(ctx, rotate);
// 绘制文本
CGContextShowTextAtPoint(ctx, 50, 300, "crazyit.org", 11);
}

大体差不多就是这样,其实就是很多API你自己拿来用就行了,只要理解那些API是什么意思,具体是设置什么属性就好了,只是这里的API有点多,所以让人感觉很繁琐,只要你有兴趣,多去尝试就能熟悉了,总之就是多玩。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值