Core Animation And Core Graphics

欧阳衡
2023-12-01

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);
}	










 类似资料:

相关阅读

相关文章

相关问答