Core Craphics

本文详细介绍了如何使用Core Graphics在iOS应用中绘制各种图形元素,包括线条、路径、矩形等,并展示了如何设置颜色、线宽及阴影效果,以及如何进行平移变换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//    画线

//    CGContextRef currentContext = UIGraphicsGetCurrentContext();

//    

//    [[UIColor redColor] set];

//    

//    CGContextSetLineWidth(currentContext, 5.0f);

//    

//    CGContextMoveToPoint(currentContext, 0, 0);

//    

//    CGContextAddLineToPoint(currentContext, 300, 400);

//    

//    CGContextSetLineJoin(currentContext, kCGLineJoinBevel);

//    

//    CGContextAddLineToPoint(currentContext, 300, 0);

//    

//    CGContextStrokePath(currentContext);

    

//    根据路径画图

//    CGMutablePathRef path = CGPathCreateMutable();

//    

//    CGRect screenBound = [[UIScreen mainScreen] bounds];

//    

//    CGPathMoveToPoint(path, NULL, screenBound.origin.x, screenBound.origin.y);

//    

//    CGPathAddLineToPoint(path, NULL, screenBound.size.width, screenBound.size.height);

//    

//    CGPathMoveToPoint(path, NULL, screenBound.size.width, screenBound.origin.y);

//    

//    CGPathAddLineToPoint(path, NULL, screenBound.origin.x, screenBound.size.height);

//    

//    CGContextRef currentContext = UIGraphicsGetCurrentContext();

//    

//    CGContextAddPath(currentContext, path);

//    

//    [[UIColor blueColor] setStroke];

//    

//    CGContextDrawPath(currentContext, kCGPathStroke);

    

//    绘制矩形

//    CGMutablePathRef path = CGPathCreateMutable();

//    

//    CGRect rectangle = CGRectMake(50.0f, 50.0f, 200, 200);

//    

//    CGPathAddRect(path, NULL, rectangle);

//    

//    CGContextRef currentContext = UIGraphicsGetCurrentContext();

//    

//    CGContextAddPath(currentContext, path);

//    

//    [[UIColor redColor] setFill];

//    

//    [[UIColor blueColor] setStroke];

//    

//    CGContextDrawPath(currentContext, kCGPathFillStroke);

//    

//    CGPathRelease(path);

    

//    绘制多个矩形

//    CGMutablePathRef path = CGPathCreateMutable();

//    

//    CGRect rect1 = CGRectMake(10, 10, 100, 100);

//    

//    CGRect rect2 = CGRectMake(200, 200, 100, 100);

//    

//    CGRect rectgroup[2] = {rect1, rect2};

//    

//    CGPathAddRects(path, NULL, rectgroup, 2);

//    

//    CGContextRef context = UIGraphicsGetCurrentContext();

//    

//    CGContextAddPath(context, path);

//    

//    [[UIColor redColor] setStroke];

//    [[UIColor blueColor] setFill];

//    

//    CGContextSetLineWidth(context, 10);

//    

//    CGContextDrawPath(context, kCGPathFillStroke);

    

//    绘制阴影

    

//    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    

//    保存以前的状态

//    CGContextSaveGState(currentContext);

//

//    CGContextSetShadowWithColor(currentContext, CGSizeMake(10.0, 10.0f), 5.0f, [[UIColor grayColor] CGColor]);

//    

//    CGMutablePathRef path = CGPathCreateMutable();

//    

//    CGRect rect1 = CGRectMake(100, 100, 100 , 100);

//    

//    CGPathAddRect(path, NULL, rect1);

//    

//    CGContextAddPath(currentContext, path);

//    

//    [[UIColor redColor] setFill];

//    [[UIColor blueColor] setStroke];

//    

//    CGContextDrawPath(currentContext, kCGPathFillStroke);

    

//    恢复原来保存的状态

//    CGContextRestoreGState(currentContext)

    

//    平移

//    CGMutablePathRef path = CGPathCreateMutable();

//    

//    CGRect rect1 = CGRectMake(10, 10, 100, 100);

//    

//    CGAffineTransform transform = CGAffineTransformMakeTranslation(100.0, 100);

//    

//    CGPathAddRect(path, &transform, rect1);

//    

//    CGContextRef currentContext = UIGraphicsGetCurrentContext();

//    

//    CGContextAddPath(currentContext, path);

//    

//    [[UIColor redColor] setStroke];

//    [[UIColor blueColor] setFill];

//    

//    CGContextSetLineWidth(currentContext, 5.0f);

//    

//    CGContextDrawPath(currentContext, kCGPathFillStroke);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值