iOS Quartz 2D绘图用CGContextRef绘制三角形 —— HERO博客
2015-12-25 15:13
621 查看
上一篇简单介绍了iOS的绘图机制和音频波形图实例,需要可以参考:iOS绘图机制简介和音频波形图实例,本篇继续练习使用。
下面用CGContextRef绘制了一个三角形,代码简单易读,先看一下效果图:
下面贴上代码:
ViewController:
下面用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. } @endHWTrigonView:
#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
相关文章推荐
- iOS 绘图机制简介,Quartz 2D绘图用CGContextRef绘制音频波形图 —— HERO博客
- 我的第一次份实习工作-iOS实习生-第二个月
- iOS Kernel Exploitation Training (April 2015)
- iOS给label添加下划线
- IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)
- ios编程之Core Data(一)
- iOS开发~CocoaPods使用详细说明
- IOS学习笔记之KVC、KVO
- 我的第一次份实习工作-iOS实习生-第一个月
- IOS Block 反向传值
- iOS: 懒加载
- 跳转到wifi设置页面 iOS
- iOS 设置导航栏的颜色和导航栏上文字的颜色
- iOS 图片拉伸不变形
- iOS 模糊效果的实现
- XCode_工程改名
- GCD使用2 详细解释 和 使用技巧
- iOS开发日记55-隐式与显式动画
- ios7中将状态栏变白的几种方式
- iOS数据存储的常用方式