【深入浅出IOS开发】IOS绘图基础

1.绘图的常用步骤

①获得设备上下文

CGContextRef ctr = UIGraphicsGetCurrentContext();

②在设备上下文绘图

CGContextMoveToPoint(ctr, 10, 10);

 CGContextAddLineToPoint(ctr, 50, 50);

③渲染到相应的View

CGContextFillPath(ctr);


一般来说绘制图形,通常是CGContextAdd*****,而更改某些属性则是CGContextSet*****.

绘制圆弧的时候,通常使用:

CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);

其中参数的设置是以x轴为0,下面为+,上面为-。

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //draw3Tri();
    //draw4Rect();
    drawEcplise();
}

void drawEcplise()
{
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    
  //  CGContextAddEllipseInRect(ctr, CGRectMake(20, 20, 100,200));
   // CGContextAddArcToPoint(ctr, 10, 10, 50, 100, 100);
    //CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);
    
    CGContextMoveToPoint(ctr, 50, 50);
    CGContextAddLineToPoint(ctr, 100, 100);
    
    CGContextSetRGBStrokeColor(ctr, 1, 0, 0, 1);
    CGContextSetLineWidth(ctr, 5);
    CGContextStrokePath(ctr);
    
    CGContextMoveToPoint(ctr, 150, 150);
    CGContextAddLineToPoint(ctr, 200, 200);
    
    CGContextSetRGBStrokeColor(ctr, 0, 0, 1, 1);
    CGContextSetLineWidth(ctr, 2);
    CGContextStrokePath(ctr);
    
    
}

void draw4Rect()
{
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    
    CGContextAddRect(ctr, CGRectMake(10, 10, 80, 80));
    
    CGContextFillPath(ctr);
}

void draw3Tri()
{
    //1.获得设备上下文
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    
    //2.在设备中绘图
    CGContextMoveToPoint(ctr, 10, 10);
    CGContextAddLineToPoint(ctr, 50, 50);
    CGContextAddLineToPoint(ctr, 100, 0);
    CGContextAddLineToPoint(ctr, 10, 10);
    //3.渲染到view中
    //CGContextStrokePath(ctr);
    CGContextFillPath(ctr);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值