iOS Core Graphics基础入门(3)

## 绘制阴影

// 创建一块画布
CGContextRef context = UIGraphicsGetCurrentContext();

// 添加阴影颜色 
CGContextSetShadowWithColor(context, CGSizeMake(1.2, 1.2), 0, [UIColor lightGrayColor].CGColor);

// 绘制字符串   
[@"Hello" drawAtPoint:CGPointMake(20, 100)
       withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:20]}];

## 绘制图片

UIImage* image = [UIImage imageNamed:@"02"];

// 第一种方法
[image drawAtPoint:CGPointMake(0, 0)];

// 第二种方法
[image drawInRect:CGRectMake(20.f, 20.0f, 320 / 2.f, 568 / 2.f)];

## 绘制屋顶

- (void)drawRect:(CGRect)rect
{
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 40.0f) textToDisplay:@"Miter" lineJoin:kCGLineJoinMiter];
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 180.0f) textToDisplay:@"Bevel" lineJoin:kCGLineJoinBevel];
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 320.0f) textToDisplay:@"Round" lineJoin:kCGLineJoinRound];
}

- (void)drawRooftopAtTopPointof:(CGPoint)paramTopPoint textToDisplay:(NSString * )paramText lineJoin:(CGLineJoin)paramLineJoin
{
    [[UIColor brownColor] set];
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineJoin(currentContext,paramLineJoin);
    CGContextSetLineWidth(currentContext, 20.0f);
    CGContextMoveToPoint(currentContext, paramTopPoint.x - 140, paramTopPoint.y + 100);
    CGContextAddLineToPoint(currentContext, paramTopPoint.x, paramTopPoint.y);
    CGContextAddLineToPoint(currentContext, paramTopPoint.x + 140, paramTopPoint.y + 100);
    CGContextStrokePath(currentContext);
    [[UIColor blackColor] set];
    [paramText drawAtPoint:CGPointMake(paramTopPoint.x - 40.0f, paramTopPoint.y + 60.0f) withFont:[UIFont boldSystemFontOfSize:30.0f]];
}

## 绘制矩形

CGContextRef context  = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();

CGPathAddRect(path, NULL, CGRectMake(0, 0, 200, 300));
CGContextAddPath(context, path);

[[UIColor colorWithRed:1 green:0 blue:0 alpha:1] setFill];
[[UIColor greenColor] setStroke];
CGContextSetLineWidth(context, 5.0f);

CGContextDrawPath(context, kCGPathFillStroke);
CGPathRelease(path);

## 绘制多个矩形

CGContextRef context  = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();

CGRect rectangle1 = CGRectMake(20.0f, 20.0f, 200.0f, 300.0f);
CGRect rectangle2 = CGRectMake(40.0f, 100.0f, 90.0f, 300.0f);
CGRect rectangles[2] = {rectangle1, rectangle2};

CGPathAddRects(path, NULL, (const CGRect *)&rectangles, 2);

CGContextAddPath(context, path);

[[UIColor colorWithRed:0 green:1 blue:0 alpha:1] setFill];
[[UIColor blackColor] setStroke];

CGContextDrawPath(context, kCGPathFillStroke);
CGPathRelease(path);

## 绘制渐变效果

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor * startColor = [UIColor blueColor];
CGFloat * startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]);
UIColor * endColor = [UIColor greenColor];
CGFloat * endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);

CGFloat colorComponents[8] = {
    startColorComponents[0],
    startColorComponents[1],
    startColorComponents[2],
    startColorComponents[3],

    endColorComponents[0],
    endColorComponents[1],
    endColorComponents[2],
    endColorComponents[3]
};

CGFloat colorIndices[2] = {0.0f,1.0f};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, (const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2);
CGColorSpaceRelease(colorSpace);

CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGPoint startPoint, endPoint;
startPoint = CGPointMake(0.0f, screenBounds.size.height/2.0f);
endPoint = CGPointMake(screenBounds.size.width, startPoint.y);

CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(currentContext,gradient , startPoint, endPoint, 0);

CGGradientRelease(gradient);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值