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

iOS Quartz 2D绘图用CGContextRef绘制三角形 —— HERO博客

2015-12-25 15:13 621 查看
上一篇简单介绍了iOS的绘图机制和音频波形图实例,需要可以参考:iOS绘图机制简介和音频波形图实例,本篇继续练习使用。

下面用CGContextRef绘制了一个三角形,代码简单易读,先看一下效果图:



下面贴上代码:

ViewController:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "HWTrigonView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//创建控件
[self creatControl];
}

- (void)creatControl
{
HWTrigonView *trigon = [[HWTrigonView alloc] initWithFrame:self.view.frame];
self.view = trigon;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
HWTrigonView:

#import <UIKit/UIKit.h>

@interface HWTrigonView : UIView

@end

#import "HWTrigonView.h"

@interface HWTrigonView ()
{
CGPoint firstPoint;
CGPoint secondPoint;
CGPoint thirdPoint;
NSMutableArray *pointArray;
}

@end

@implementation HWTrigonView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
pointArray = [[NSMutableArray alloc] initWithCapacity:3];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 320, 40)];
label.text = @"单机屏幕内的3个点来绘制一个三角形";
[self addSubview:label];
}
return self;
}

- (void)drawRect:(CGRect)rect
{
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//创建路径
CGMutablePathRef path = CGPathCreateMutable();

if (pointArray.count == 1) {
firstPoint = [[pointArray objectAtIndex:0] CGPointValue];
//画点
CGContextFillEllipseInRect(context, CGRectMake(firstPoint.x, firstPoint.y, 2.0, 2.0));

}else if(pointArray.count == 2) {
secondPoint = [[pointArray objectAtIndex:1] CGPointValue];
//起点坐标
CGPathMoveToPoint(path, nil, firstPoint.x, firstPoint.y);
//下一个点坐标
CGPathAddLineToPoint(path, nil, secondPoint.x, secondPoint.y);

}else if(pointArray.count == 3) {
thirdPoint = [[pointArray objectAtIndex:2] CGPointValue];
CGPathMoveToPoint(path, nil, firstPoint.x, firstPoint.y);
CGPathAddLineToPoint(path, nil, secondPoint.x, secondPoint.y);
CGPathAddLineToPoint(path, nil, thirdPoint.x, thirdPoint.y);
CGPathAddLineToPoint(path, nil, firstPoint.x, firstPoint.y);
//填充颜色
CGContextSetRGBFillColor(context, arc4random_uniform(256) / 255.0, arc4random_uniform(256) / 255.0, arc4random_uniform(256) / 255.0, 1);
}
//添加路径到图形上下文
CGContextAddPath(context, path);
//设置线条宽度
CGContextSetLineWidth(context, 2.0);
//设置线条颜色
CGContextSetRGBStrokeColor(context, arc4random_uniform(256) / 255.0, arc4random_uniform(256) / 255.0, arc4random_uniform(256) / 255.0, 0.5);
//设置阴影
CGContextSetShadowWithColor(context, CGSizeMake(2.0, 2.0), 0.7, [UIColor grayColor].CGColor);
//绘制图像到指定图形上下文
CGContextDrawPath(context, kCGPathFillStroke);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[pointArray addObject:[NSValue valueWithCGPoint:point]];

if (pointArray.count > 3) {
[pointArray removeObjectsInRange:NSMakeRange(0, 3)];
firstPoint = [[pointArray objectAtIndex:0] CGPointValue];
}

[self setNeedsDisplay];
}

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