Core Animation And Core Graphics

UIKit

The high-level framework that allows developers to create views and UI related components. 

It also incorporates some of the low-level APIs into an easier-to-use high-level API.


Quartz 2D

The main engine running under the hood to facilitate drawing in iOS; UIKit uses Quartz.


Core Graphics
A framework that supports the graphics context (more on this later), loading images, drawing images, and so on.


Core Animation
A framework that, as its name implies, facilitates animations in iOS.


绘制字符串

- (void)drawRect:(CGRect)rect{ // Drawing code
UIFont *helveticaBold =
[UIFont fontWithName:@"HelveticaNeue-Bold"
size:40.0f]; NSString *myString = @"Some String";
[myString drawAtPoint:CGPointMake(40, 180) withFont:helveticaBold];
}

UIColor is really a UIKit wrapper around the Core Graphics class CGColor. 

UIColor *steelBlueColor = [UIColor colorWithRed:0.3f green:0.4f blue:0.6f alpha:1.0f];
CGColorRef colorRef = [steelBlueColor CGColor];

绘制图像

- (void)drawRect:(CGRect)rect{ // Drawing code
/* Assuming the image is in your app bundle and we can load it */
UIImage *xcodeIcon =
[UIImage imageNamed:@"Xcode.png"];
[xcodeIcon drawAtPoint:CGPointMake(0.0f, 20.0f)];
[xcodeIcon drawInRect:CGRectMake(50.0f, 10.0f, 40.0f,
35.0f)];
}

绘制线条

path和line的区别,lines have to be drawn using paths.定义paths,然后让Core Graphics进行绘制

- (void)drawRect:(CGRect)rect{ // Drawing code
/* Set the color that we want to use to draw the line */ 
[[UIColor brownColor] set];
/* Get the current graphics context */
 CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Set the width for the line */ CGContextSetLineWidth(currentContext, 5.0f);
/* Start the line at this point */ CGContextMoveToPoint(currentContext, 50.0f, 10.0f);
/* And end it at this point */ CGContextAddLineToPoint(currentContext, 100.0f, 200.0f);
/* Use the context's current color to draw the line */
CGContextStrokePath(currentContext);
 }


你可以设置lines之间的节点类型

CGContextSetLineJoin(currentContext,paramLineJoin);

Paths belong to whichever graphics context they are drawn on. Paths do not have boundaries or specific shapes, unlike the shapes we draw on them. But paths do have bounding boxes. Bounding box of a path is the smallest rectangle that contains all the shapes, points, and other objects that have been drawn on that specific path. 

设置path

- (void)drawRect:(CGRect)rect
{
	CGContextRef currentContext = UIGraphicsGetCurrentContext();
	
	CGMutablePathRef path = CGPathCreateMutable();
	
	CGPathMoveToPoint(path, NULL, self.bounds.origin.x, self.bounds.origin.y);
	CGPathAddLineToPoint(path, NULL, self.bounds.size.width, self.bounds.size.height);
	
	CGPathMoveToPoint(path, NULL, self.bounds.size.width, self.bounds.origin.y);
	CGPathAddLineToPoint(path, NULL, self.bounds.origin.x, self.bounds.size.height);
	
	CGContextSetLineWidth(currentContext, 5.0);
	[[UIColor purpleColor] set];

	CGContextAddPath(currentContext, path);
	CGContextDrawPath(currentContext, kCGPathStroke);
	CGPathRelease(path);
}	

- (void)drawRect:(CGRect)rect
{
	CGContextRef currentContext = UIGraphicsGetCurrentContext();
	
	CGMutablePathRef path = CGPathCreateMutable();
	
	CGRect rectangle = CGRectMake(30.0, 30.0, 100.0, 200.0);
	CGPathAddRect(path, NULL, rectangle);
	
	[[UIColor blackColor] setStroke];
	CGContextSetLineWidth(currentContext, 5.0);
	
	[[UIColor purpleColor] setFill];
	
	CGContextAddPath(currentContext, path);
	CGContextDrawPath(currentContext, kCGPathFillStroke);
	CGPathRelease(path);
}	










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值