码迷,mamicode.com
首页 > 其他好文 > 详细

绘制简单图形

时间:2015-11-23 18:31:42      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

1.三角形

- (void)drawRect:(CGRect)rect {
    // Drawing code
//    1.获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    2.拼接图形
    CGContextMoveToPoint(ctx, 10,10);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 150, 40);
//    CGContextAddLineToPoint(ctx, 10, 10);
    CGContextClosePath(ctx);
// 绘制图形空心

CGContextStrokePath(ctx);

//绘制图形实心

 CGContextFillPath(ctx);


}

2.四边形

- (void)drawRect:(CGRect)rect {
    // Drawing code
//    1.获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    2.拼接图形
    CGContextAddRect(ctx, CGRectMake(10, 10, 100, 100));
//    3.显示图形空心
    CGContextStrokePath(ctx);

//     绘制图形实心

     CGContextFillPath(ctx);
}

- (void)drawRect:(CGRect)rect {
    // 1.获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 2.拼接图形
    
    //设置线段宽度
    CGContextSetLineWidth(ctx, 10);
    
    //设置线段头尾的样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    //设置颜色
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
    
 
    //设置起点
    CGContextMoveToPoint(ctx, 10, 10);
    
    //加一条线段
    CGContextAddLineToPoint(ctx, 100, 100);
    
    //渲染一次
    CGContextStrokePath(ctx);
    
   
    //设置颜色
    CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);
    
    //设置起点
    CGContextMoveToPoint(ctx, 200, 190);
    
    //加一条线段
    CGContextAddLineToPoint(ctx, 150, 40);
    
    //渲染一次
    CGContextStrokePath(ctx);

    
    
}

- 圆

- (void)drawRect:(CGRect)rect {
    // 1.获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.拼接图形
    CGContextAddEllipseInRect(ctx, CGRectMake(10, 10, 150, 50));
    
    [[UIColor redColor]set];
    
    CGContextSetLineWidth(ctx, 5);
    
    // 3.显示图形
    CGContextStrokePath(ctx);

}
-圆弧

- (void)drawRect:(CGRect)rect {
    // 1.获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.画圆弧
    //x/y圆心
    //radius 半径
    //startAngle 开始角度
    //endAngle 结束角度
    //clockwise 圆弧伸展方向(0顺时针 1逆时针)
    CGContextAddArc(ctx,50,50,40,-M_PI_2, M_PI_2, 1);
    // 3.显示图形
    CGContextStrokePath(ctx);
}
- (void)drawRect:(CGRect)rect
{
    // 1.获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 2.画1/4圆
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 100, 150);
    CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI, 1);
    CGContextClosePath(ctx);
    
    [[UIColor redColor] set];
    
    // 3.显示所绘制的东西
    CGContextFillPath(ctx);
}


绘制简单图形

标签:

原文地址:http://www.cnblogs.com/zep908323412/p/4989104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!