Quartz2D 基本操作

来源:互联网 发布:淘宝宜家代购真假辨别 编辑:程序博客网 时间:2024/06/05 14:57

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
原创粉丝点击