本文轉(zhuǎn)載自:http://blog.csdn.net/rhljiayou/article/details/9919713
首先了解一下CGContextRef:
An opaque type that represents a Quartz 2D drawing environment.
Graphics Context是圖形上下文,可以將其理解為一塊畫(huà)布,我們可以在上面進(jìn)行繪畫(huà)操作,繪制完成后,將畫(huà)布放到我們的view中顯示即可,view看作是一個(gè)畫(huà)框.
看一下demo效果圖先:
?
自定義CustomView類(lèi),CustomView.h:
?
[cpp]?view plaincopy
#import?<UIKit/UIKit.h>??#import?<QuartzCore/QuartzCore.h>??#define?PI?3.14159265358979323846??@interface?CustomView?:?UIView??????@end??
實(shí)現(xiàn)類(lèi)CustomView.m:
?
?
[cpp]?view plaincopy
#import?"CustomView.h"????@implementation?CustomView????-?(id)initWithFrame:(CGRect)frame??{??????self?=?[super?initWithFrame:frame];??????if?(self)?{??????}??????return?self;??}??????-?(void)drawRect:(CGRect)rect??{??????????????CGContextRef?context?=?UIGraphicsGetCurrentContext();????????????????CGContextSetRGBFillColor?(context,??1,?0,?0,?1.0);????UIFont??*font?=?[UIFont?boldSystemFontOfSize:15.0];????[@"畫(huà)圓:"?drawInRect:CGRectMake(10,?20,?80,?20)?withFont:font];??????[@"畫(huà)線(xiàn)及孤線(xiàn):"?drawInRect:CGRectMake(10,?80,?100,?20)?withFont:font];??????[@"畫(huà)矩形:"?drawInRect:CGRectMake(10,?120,?80,?20)?withFont:font];??????[@"畫(huà)扇形和橢圓:"?drawInRect:CGRectMake(10,?160,?110,?20)?withFont:font];??????[@"畫(huà)三角形:"?drawInRect:CGRectMake(10,?220,?80,?20)?withFont:font];??????[@"畫(huà)圓角矩形:"?drawInRect:CGRectMake(10,?260,?100,?20)?withFont:font];??????[@"畫(huà)貝塞爾曲線(xiàn):"?drawInRect:CGRectMake(10,?300,?100,?20)?withFont:font];??????[@"圖片:"?drawInRect:CGRectMake(10,?340,?80,?20)?withFont:font];????????????????CGContextSetRGBStrokeColor(context,1,1,1,1.0);????CGContextSetLineWidth(context,?1.0);????????????CGContextAddArc(context,?100,?20,?15,?0,?2*PI,?0);?????CGContextDrawPath(context,?kCGPathStroke);???????????????CGContextAddArc(context,?150,?30,?30,?0,?2*PI,?0);?????CGContextDrawPath(context,?kCGPathFill);??????????????UIColor*aColor?=?[UIColor?colorWithRed:1?green:0.0?blue:0?alpha:1];??????CGContextSetFillColorWithColor(context,?aColor.CGColor);????CGContextSetLineWidth(context,?3.0);????CGContextAddArc(context,?250,?40,?40,?0,?2*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?*?PI?/?180,?-120?*?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);???????????????????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:@"apple.jpg"];??????[image?drawInRect:CGRectMake(60,?340,?20,?20)];????CGContextDrawImage(context,?CGRectMake(100,?340,?20,?20),?image.CGImage);????????}??????@end??
用法:
?
?
[cpp]?view plaincopy
CustomView?*customView?=?[[CustomView?alloc]initWithFrame:CGRectMake(0,?0,?320,?self.view.frame.size.height)];??????[self.view?addSubview:customView]; ?
轉(zhuǎn)載于:https://www.cnblogs.com/liuting-1204/p/5757203.html
總結(jié)
以上是生活随笔為你收集整理的IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。