您的位置:首页 > 移动开发 > IOS开发

iOS CGContextRef的使用

2016-05-30 16:54 411 查看
今天有空尝试了iOS画图,还蛮好玩的,我就画了几个。

创建一个类 继承View

//
//  CustomView.m
//  CollectionCuttingLine
//
//  Created by GongHui_YJ on 16/5/30.
//  Copyright © 2016年 YangJian. All rights reserved.
//

#import "CustomView.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation CustomView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//        [self drawRect:frame];
}
return self;
}

/**
*  这里绘制
*
*  @param rect
*/
- (void)drawRect:(CGRect)rect
{
// 一个不透明类型的Quartz 2D 会话环境, 相当于一个画布,可以
CGContextRef context = UIGraphicsGetCurrentContext(); // 设置上下文
// 画一条线
//    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5); // 设置颜色 透明度
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); // 线条颜色
CGContextSetLineWidth(context, 0.1); // 线条的宽度
CGContextMoveToPoint(context, 0, 300); // 开始绘制 x,y 为开始点的坐标
CGContextAddLineToPoint(context, 200, 300); // 画直线, x,y 为线条结束点的坐标
CGContextStrokePath(context); // 开始绘制

// 绘制贝兹曲线
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10, 100);
CGContextAddCurveToPoint(context, 200, 50, 100, 400, 300, 400);
CGContextStrokePath(context);

// 绘制连续的曲线
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextMoveToPoint(context, 230, 150);
CGContextAddCurveToPoint(context, 310, 100, 300, 200, 220, 220);// 画三次点曲线
CGContextAddCurveToPoint(context, 290, 140, 280, 180, 240, 190); // 画三次点曲线
CGContextStrokePath(context);

// 绘制虚虚线
CGContextSetRGBStrokeColor(context, 0.1, 0.2, 0.3, 1); // 线条颜色
CGFloat dashArray1[] = {10, 10}; // 表示先绘制10个点,再跳过10个点
CGContextSetLineWidth(context, 0.3); // 设置线的宽度
CGContextSetLineDash(context, 0, dashArray1, 2); // 画虚线
CGContextMoveToPoint(context, 10, 130); // 开始绘制 开始点的坐标
CGContextAddLineToPoint(context, 300, 130); // // 绘制 结束点的坐标
CGContextStrokePath(context);

// 绘制连续的虚线曲线
CGContextSetLineWidth(context, 0.5);
CGFloat dashArray2[] = {5,3,10};
CGContextSetLineDash(context, 0, dashArray2, 3);
CGContextMoveToPoint(context, 5, 120);
CGContextAddCurveToPoint(context, 200, 50, 100, 400, 300, 400);
CGContextStrokePath(context);

//绘制连续的曲线
CGContextSetLineWidth(context, 5.0);
CGFloat dashArray3[] = {3, 2, 10, 20, 5};
CGContextSetLineDash(context, 0, dashArray3, 5);//画虚线
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextMoveToPoint(context, 5, 400);//开始画线, x,y 为开始点的坐标
CGContextAddCurveToPoint(context, 50, 200, 80, 300, 100, 220);//画三次点曲线
CGContextAddQuadCurveToPoint(context, 150, 100, 200, 200);//画二次点曲线
CGContextAddCurveToPoint(context, 240, 400, 10, 50, 300, 300);//画三次点曲线
CGContextStrokePath(context);//开始画线

// 画一个方形图形 没有边框
CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); // 方框的填充色
CGContextFillRect(context, CGRectMake(30, 200, 200, 100)); // 画一个方框

// 画弧线
CGContextSetRGBStrokeColor(context, 0.3, 0.4, 0.5, 1);// 线条色艳
CGContextAddArc(context, 180, 300, 50, 0, 180*(M_PI/180), 0);
CGContextStrokePath(context);

//画方形边框
CGContextRef context5 = UIGraphicsGetCurrentContext(); //设置上下文
CGContextSetLineWidth(context5, 3.0);
CGContextSetRGBStrokeColor(context5, 0.8, 0.1, 0.8, 1);
CGContextStrokeRect(context5, CGRectMake(5, 5, 300, 400));//画方形边框, 参数2:方形的坐标。

//画椭圆
CGRect aRect= CGRectMake(80, 80, 160, 100);
CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextAddEllipseInRect(context, aRect); //椭圆, 参数2:椭圆的坐标。
CGContextDrawPath(context, kCGPathStroke);

//画实心圆
CGContextFillEllipseInRect(context, CGRectMake(95, 195, 200.0, 100));//画实心圆,参数2:圆坐标

//画一个菱形
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);

//填充了一段路径:
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  绘图 画图