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

上一篇简单介绍了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



  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值