CoreGraphics.framework 是iOS 內置的用於畫圖的框架,能夠畫自定義的幾何圖形,它支持圖形上下文、加載圖像、繪製圖像,等等。 xcode
下面是我今天練習的代碼: 框架
(1)代碼1:繪製字符串 code
- (void)drawRect:(CGRect)rect { // Drawing code UIColor * magentaColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f]; [magentaColor set]; UIFont * helveticaBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0f]; NSString * myString = @"I Learn Really Fast"; [myString drawAtPoint:CGPointMake(25, 190) withFont:helveticaBold]; }(2)代碼2:繪製圖像
-(void)drawRect:(CGRect)rect { UIImage * image = [UIImage imageNamed:@"xcode.png"]; if(image != nil) { NSLog(@"Successfully loaded the image"); } else { NSLog(@"Failed to load the image"); } [image drawAtPoint:CGPointMake(0.0f, 20.0f)]; [image drawInRect:CGRectMake(50.0f, 10.0f, 40.0f,35.0f)]; }(3)代碼3:繪製線段
-(void)drawRect:(CGRect)rect { [[UIColor brownColor] set]; CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(currentContext, 5.0f); CGContextMoveToPoint(currentContext, 50.0f, 10.0f); CGContextAddLineToPoint(currentContext, 100.0f, 200.0f); CGContextStrokePath(currentContext); }(4)繪製兩條相連的線段
-(void)drawRect:(CGRect)rect { [[UIColor brownColor] set]; CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(currentContext, 5.0f); CGContextMoveToPoint(currentContext, 20.0f, 20.0f); CGContextAddLineToPoint(currentContext, 100.0f, 100.0f); CGContextAddLineToPoint(currentContext, 300.0f, 100.0f); CGContextStrokePath(currentContext); }(5)繪製屋頂demo
-(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]]; }(6)繪製矩形
-(void)drawRect:(CGRect)rect { CGMutablePathRef path = CGPathCreateMutable(); CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f); CGPathAddRect(path, NULL, rectangle); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextAddPath(currentContext, path); [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill]; [[UIColor brownColor] setStroke]; CGContextSetLineWidth(currentContext, 5.0f); CGContextDrawPath(currentContext, kCGPathFillStroke); CGPathRelease(path); }(7)同時繪製多個矩形
-(void)drawRect:(CGRect)rect { CGMutablePathRef path = CGPathCreateMutable(); CGRect rectangle1 = CGRectMake(10.0f, 10.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); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextAddPath(currentContext, path); [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill]; [[UIColor blackColor] setStroke]; CGContextDrawPath(currentContext, kCGPathFillStroke); CGPathRelease(path); }(8)給幾何圖形添加陰影
-(void)drawRectAtTopOfScreen { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSaveGState(currentContext); CGContextSetShadowWithColor(currentContext, CGSizeMake(10.0f, 10.0f), 20.0f, [[UIColor grayColor] CGColor]); CGMutablePathRef path = CGPathCreateMutable(); CGRect firstRect = CGRectMake(55.0f, 60.0f, 150.0f, 150.0f); CGPathAddRect(path, NULL, firstRect); CGContextAddPath(currentContext, path); [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill]; CGContextDrawPath(currentContext, kCGPathFill); CGPathRelease(path); CGContextRestoreGState(currentContext); } -(void)drawRectAtBottomOfScreen { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGMutablePathRef path = CGPathCreateMutable(); CGRect secondRect = CGRectMake(150.0f, 250.0f, 100.0f, 100.0f); CGPathAddRect(path, NULL , secondRect); CGContextAddPath(currentContext, path); [[UIColor purpleColor] setFill]; CGContextDrawPath(currentContext, kCGPathFill); CGPathRelease(path); }