您的位置:首页 > 其它

Quartz2D-基本图形绘制

2016-07-27 22:22 288 查看
Quartz2D简单实用

//
// GRedView.m
// Quartz2D-基本图形绘制
//
// Created by gaocai on 16/7/27.
// Copyright © 2016年 gaocai. All rights reserved.
//

#import "GRedView.h"

@implementation GRedView

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

CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat start = 0;
CGFloat end = M_PI;

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:start endAngle:end clockwise:YES];
//内部自动调用上下文
// [path stroke];
[path fill];
}

- (void)drawRect {

//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.获取描述路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
//3.吧路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.将上下文的内容显示到View上
CGContextFillPath(ctx);
}

/**
* 画曲线
*/
- (void)drawQuadCurve {
//1获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2获取描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//设置起点
[path moveToPoint:CGPointMake(20, 100)];
//添加另一点还有一个控制点
[path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
//3把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//把上下文的内容显示到View上
CGContextStrokePath(ctx);

}

/**
* 画线
*/
- (void)drawLine{
//1首先取得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//2其次获得一个描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//设置线的起点
[path moveToPoint:CGPointMake(10, 100)];
//链接线的第二个点
[path addLineToPoint:CGPointMake(200, 20)];
//当添加第三个点时会把第二个点当成起始点
[path addLineToPoint:CGPointMake(100, 200)];
//同上
[path addLineToPoint:CGPointMake(10, 100)];

//设置线的宽度
CGContextSetLineWidth(ctx, 10);
//设置顶点的角为圆角
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//设置起始点为圆角
CGContextSetLineCap(ctx, kCGLineCapRound);
//空心时的线颜色
[[UIColor yellowColor] setStroke];
// [[UIColor yellowColor] setFill];//实心时线颜色 和CGContextFillPath对应

//3然后将描绘的路径添加到上下文中
CGContextAddPath(ctx, path.CGPath);

//4最后渲染到View上
CGContextStrokePath(ctx);//空心
// CGContextFillPath(ctx);//实心

}

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