Quartz2D 基本操作

drawRect:方法会在view第一次显示和刷新的时候调用,这个方法是系统自动调用的,程序员不能手动调用。


基本操作

CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 保存图形上下文
    CGContextSaveGState(ctx);
    
    // 画线
    CGContextMoveToPoint(ctx, 10, 10);
    CGContextAddLineToPoint(ctx, 180, 120);
    
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextClosePath(ctx);
    
    //    CGContextStrokePath(ctx); // 空心
    CGContextFillPath(ctx); // 实心
    
    // 画线
    CGContextMoveToPoint(ctx, 150, 140);
    CGContextAddLineToPoint(ctx, 290, 340);
    CGContextAddLineToPoint(ctx, 200, 340);
    
    // 设置线宽
    CGContextSetLineWidth(ctx, 15);
    
    // 设置颜色
    [[UIColor orangeColor] set];
    
    // 设置线头样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    // 设置转角样式
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    
    CGContextStrokePath(ctx);
    
    // 获取栈顶的图形上下文
    CGContextRestoreGState(ctx);
    
    // 画圆
    CGContextAddArc(ctx, 200, 200, 50, 0, M_PI * 2, 0); // 方法一
    
    // 方法二
    CGContextAddEllipseInRect(ctx, CGRectMake(50, 400, 100, 100));
    
    CGContextStrokePath(ctx);
    
    // 画圆弧
    CGContextAddArc(ctx, 30, 300, 50, 0, M_PI_2, 0);
    
    //    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);
    
    // 矩阵操作
    CGContextRotateCTM(ctx, M_PI_4 / 4); // 旋转
    CGContextScaleCTM(ctx, 0.5, 0.5); // 缩放
    
    CGContextAddRect(ctx, CGRectMake(200, 270, 60, 150));
    
    CGContextStrokePath(ctx);

裁剪操作

CGContextRef ctx = UIGraphicsGetCurrentContext(); // 获取图形上下文
    
    CGContextAddEllipseInRect(ctx, CGRectMake(10, 50, 250, 250));
    
    CGContextClip(ctx); // 裁剪
    
    UIImage *image = [UIImage imageNamed:@"test"];
    
    [image drawAtPoint:CGPointMake(10, 50)];

重绘

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext(); // 获取图形上下文
    
    CGContextAddArc(ctx, 180, 220, self.radius, 0, M_PI * 2, 0);
    
    CGContextStrokePath(ctx);
    
}

- (void)setRadius:(int)radius {
    _radius = radius;
    
    [self setNeedsDisplay];
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值