ios中Quartz2D应用

1.画线

- (void)drawRect:(CGRect)rect {
    // Drawing code
    //绘一条
    
    //获取上下文 上下文的输出目标就是self[view]
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置线颜色
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
    
    // 设置线宽
    CGContextSetLineWidth(context, 13);
    
    
    // 设置线的头尾的样式
    CGContextSetLineCap(context, kCGLineCapButt);
    
    // 设置连接点的样式
    CGContextSetLineJoin(context, kCGLineJoinRound);
    
    //画一条线
    //设置一起点
    CGContextMoveToPoint(context, 10, 10);
    //设置连线另一个点
    CGContextAddLineToPoint(context, 30, 100);
    CGContextAddLineToPoint(context, 110, 110);
    
    //画到view [渲染]
    CGContextStrokePath(context);
    
}

2.画矩形

-(void)drawRectangle{
    //绘一条
    
    //获取上下文 上下文的输出目标就是self[view]
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置线颜色
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
    
    // 设置线宽
    //CGContextSetLineWidth(context, 13);
    
    
    
    //画一个矩开
    
    //=======第一方法============
    //    //设置一起点
    //    CGContextMoveToPoint(context, 10, 10);
    //    //设置另外三个点
    //
    //    CGContextAddLineToPoint(context, 110, 10);
    //    CGContextAddLineToPoint(context, 110, 110);
    //    CGContextAddLineToPoint(context, 10, 110);
    //    CGContextAddLineToPoint(context, 10, 10);
    
    
    //=======第一方法============
    CGContextAddRect(context, CGRectMake(10, 10, 100, 100));
    
    //画到view [渲染]
    //只是画一条,【空心】
    //CGContextStrokePath(context);
    
    //填充 【实心】
    CGContextFillPath(context);
}
3.画三角形
-(void)drawTriangle{
    //获取上下文 上下文的输出目标就是self[view]
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置线颜色
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
    
    CGContextMoveToPoint(context, 10, 10);
    //设置另外三个点

    CGContextAddLineToPoint(context, 110, 10);
    CGContextAddLineToPoint(context, 110, 110);
    //CGContextAddLineToPoint(context, 10, 10);
    //关闭路径
    CGContextClosePath(context);
    
    // 渲染
    CGContextStrokePath(context);
}
4.画圆
-(void)drawCircle{
    //上下文
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    //画圈
    CGContextAddEllipseInRect(context, CGRectMake(10, 10, 100, 100));
    
    //渲染
    CGContextStrokePath(context);
}
5.画弧

-(void)drawArc{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    /**
     *x,y 圆心
     *radius 半径
     *startAngle 画弧的起始位置
     *endAngel 画弧的结束位置
     * clockwise 0 顺针 1 逆时针
     */
    CGContextAddArc(context, 100, 100, 60, 0, M_PI, 1);
    
    CGContextClosePath(context);
    
    //渲染
    CGContextStrokePath(context);
    //CGContextFillPath(context);
}

6.画扇形

-(void)drawSector{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //设置一个起点
    CGContextMoveToPoint(context, 100, 100);
    
    CGContextAddArc(context, 100, 100, 60, - M_PI_4, - 3 * M_PI_4, 1);
   
    CGContextClosePath(context);
    
    CGContextStrokePath(context);

}
7.画文字


8.画图片

//画图片
    UIImage *image = [UIImage imageNamed:@"papa"];
    //[image drawAtPoint:CGPointZero];
    //[image drawInRect:CGRectMake(10, 10, 50, 50)];
    
    //平铺
    [image drawAsPatternInRect:CGRectMake(0, 0, 180, 180)];


9.画进度圈

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGFloat textH = 20;
    CGFloat textW = 30;
    CGFloat viewW = rect.size.width;
    CGFloat viewH = rect.size.height;
    
    CGFloat textX = (viewW - textW) * 0.5;
    CGFloat textY = (viewH - textH) * 0.5;
    
    // 1.画文字
    NSString *text = [NSString stringWithFormat:@"%.2f",self.pregress];
    [text drawInRect:CGRectMake(textX, textY, textW, textH) withAttributes:nil];
    
    
    // 2.画弧
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //半径
    CGFloat radius = (viewW - 10) * 0.5;
    
    CGFloat endAngle = self.pregress * 2 * M_PI - M_PI_4;
    
    CGContextAddArc(context, viewW * 0.5, viewH * 0.5, radius, - M_PI_4, endAngle, 0);
    CGContextStrokePath(context);
    
}


-(void)setPregress:(float)pregress{
    _pregress = pregress;
    
    //重绘
    [self setNeedsDisplay];
}





















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值