iOS-CGContextRef画各种图形例子
绘制效果图
绘制代码
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15.0], NSFontAttributeName, [UIColor redColor], NSForegroundColorAttributeName, nil];
[@"画圆:" drawInRect:CGRectMake(10, 20, 80, 20) withAttributes:dic];
[@"画线及孤线:" drawInRect:CGRectMake(10, 80, 100, 20) withAttributes:dic];
[@"画矩形:" drawInRect:CGRectMake(10, 120, 80, 20) withAttributes:dic];
[@"画扇形和椭圆:" drawInRect:CGRectMake(10, 160, 110, 20) withAttributes:dic];
[@"画三角形:" drawInRect:CGRectMake(10, 220, 80, 20) withAttributes:dic];
[@"画圆角矩形:" drawInRect:CGRectMake(10, 260, 100, 20) withAttributes:dic];
[@"画贝塞尔曲线:" drawInRect:CGRectMake(10, 300, 100, 20) withAttributes:dic];
[@"图片:" drawInRect:CGRectMake(10, 340, 80, 20) withAttributes:dic];
CGContextSetRGBStrokeColor(context,1,1,1,1.0);
CGContextSetLineWidth(context, 1.0);
CGContextAddArc(context, 100, 20, 15, 0, 2*M_PI, 0);
CGContextDrawPath(context, kCGPathStroke);
UIColor*aColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddArc(context, 150, 30, 30, 0, 2*M_PI, 0);
CGContextDrawPath(context, kCGPathFill);
CGContextSetLineWidth(context, 3.0);
CGContextAddArc(context, 250, 40, 40, 0, 2*M_PI, 0);
CGContextDrawPath(context, kCGPathFillStroke);
CGPoint aPoints[2];
aPoints[0] =CGPointMake(100, 80);
aPoints[1] =CGPointMake(130, 80);
CGContextAddLines(context, aPoints, 2);
CGContextDrawPath(context, kCGPathStroke);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
CGContextMoveToPoint(context, 140, 80);
CGContextAddArcToPoint(context, 148, 68, 156, 80, 10);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 160, 80);
CGContextAddArcToPoint(context, 168, 68, 176, 80, 10);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 150, 90);
CGContextAddArcToPoint(context, 158, 102, 166, 90, 10);
CGContextStrokePath(context);
CGContextStrokeRect(context,CGRectMake(100, 120, 10, 10));
CGContextFillRect(context,CGRectMake(120, 120, 10, 10));
CGContextSetLineWidth(context, 2.0);
aColor = [UIColor blueColor];
CGContextSetFillColorWithColor(context, aColor.CGColor);
aColor = [UIColor yellowColor];
CGContextSetStrokeColorWithColor(context, aColor.CGColor);
CGContextAddRect(context,CGRectMake(140, 120, 60, 30));
CGContextDrawPath(context, kCGPathFillStroke);
CAGradientLayer *gradient1 = [CAGradientLayer layer];
gradient1.frame = CGRectMake(240, 120, 60, 30);
gradient1.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,
(id)[UIColor grayColor].CGColor,
(id)[UIColor blackColor].CGColor,
(id)[UIColor yellowColor].CGColor,
(id)[UIColor blueColor].CGColor,
(id)[UIColor redColor].CGColor,
(id)[UIColor greenColor].CGColor,
(id)[UIColor orangeColor].CGColor,
(id)[UIColor brownColor].CGColor,nil];
[self.layer insertSublayer:gradient1 atIndex:0];
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
1,1,1, 1.00,
1,1,0, 1.00,
1,0,0, 1.00,
1,0,1, 1.00,
0,1,1, 1.00,
0,1,0, 1.00,
0,0,1, 1.00,
0,0,0, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents
(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 220, 90);
CGContextAddLineToPoint(context, 240, 90);
CGContextAddLineToPoint(context, 240, 110);
CGContextAddLineToPoint(context, 220, 110);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient,CGPointMake
(220,90) ,CGPointMake(240,110),
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 260, 90);
CGContextAddLineToPoint(context, 280, 90);
CGContextAddLineToPoint(context, 280, 100);
CGContextAddLineToPoint(context, 260, 100);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient,CGPointMake
(260, 90) ,CGPointMake(260, 100),
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
CGContextDrawRadialGradient(context, gradient, CGPointMake(300, 100), 0.0, CGPointMake(300, 100), 10, kCGGradientDrawsBeforeStartLocation);
aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextMoveToPoint(context, 160, 180);
CGContextAddArc(context, 160, 180, 30, -60 * M_PI / 180, -120 * M_PI / 180, 1);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextAddEllipseInRect(context, CGRectMake(160, 180, 20, 8));
CGContextDrawPath(context, kCGPathFillStroke);
CGPoint sPoints[3];
sPoints[0] =CGPointMake(100, 220);
sPoints[1] =CGPointMake(130, 220);
sPoints[2] =CGPointMake(130, 160);
CGContextAddLines(context, sPoints, 3);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
float fw = 180;
float fh = 280;
CGContextMoveToPoint(context, fw, fh-20);
CGContextAddArcToPoint(context, fw, fh, fw-20, fh, 10);
CGContextAddArcToPoint(context, 120, fh, 120, fh-20, 10);
CGContextAddArcToPoint(context, 120, 250, fw-20, 250, 10);
CGContextAddArcToPoint(context, fw, 250, fw, fh-20, 10);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 3);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) byRoundingCorners:(UIRectCornerTopLeft |UIRectCornerTopRight |UIRectCornerBottomRight) cornerRadii:CGSizeMake(10, 10)];
[path stroke];
CGContextMoveToPoint(context, 120, 300);
CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 200, 300);
CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);
CGContextStrokePath(context);
UIImage *image = [UIImage imageNamed:@"image.jpg"];
[image drawInRect:CGRectMake(60, 340, 20, 20)];
CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), image.CGImage);
CGContextDrawTiledImage(context, CGRectMake(0, 0, 20, 20), image.CGImage);
}
转载地址
http://blog.csdn.net/rhljiayou/article/details/9919713