/**
* 绘制字符串
*
* @param str 要绘制的字符串对象
*/
- (void)drawString:(NSString *)str{
// 在屏幕 x = 40 y = 180的地方绘制一个简单的字符串
// 设置字体样式
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20.f];
// 设置画笔颜色
UIColor *magentaColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
NSDictionary *attributesDic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,magentaColor ,NSForegroundColorAttributeName,nil];
// [myString drawAtPoint:CGPointMake(40, 180) withAttributes:attributesDic];
[str drawInRect:CGRectMake(100, 120, 100, 320) withAttributes:attributesDic];
}
/**
* 获取颜色的R/G/B alpha等分量值
*
* @param color 颜色对象
*/
- (void)getColorCommpent:(UIColor *)color{
CGColorRef colorRef = [color CGColor];
const CGFloat *components = CGColorGetComponents(colorRef);
NSUInteger componentsCount = CGColorGetNumberOfComponents(colorRef);
NSUInteger counter = 0;
for (counter = 0; counter < componentsCount; counter ++) {
NSLog(@"Component %lu = %.02f",(unsigned long)counter + 1,components[counter]);
}
}
/**
* 利用UIImage对象绘制图片
*
* @param imgName 图片名称
*/
- (void)drawImage:(NSString *)imgName{
UIImage *image = [UIImage imageNamed:imgName];
if (image != nil) {
NSLog(@"Successfully loaded the image.");
} else {
NSLog(@"Failed to load the image.");
}
[image drawAtPoint:CGPointMake(0, 0)];
[image drawInRect:CGRectMake(100, 120, 190, 200)];
}
/**
* 利用图形上下文间接操作路径绘制线条
*/
- (void)drawLine{
[[UIColor redColor] set];
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext,10.0f);
CGContextMoveToPoint(currentContext, 50.f, 10.f);
CGContextAddLineToPoint(currentContext, 100.f, 200.f);
CGContextAddLineToPoint(currentContext, 200.f, 300.f);
CGContextStrokePath(currentContext);
}
/**
* 展示连接的几种类型,通俗将来就是 转角 CGLineJoin属性的实践
*
* @param paramTopPoint 顶端的点
* @param paramText 每种类型文本
* @param paramLineJoin 具体类型的属性
*/
- (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 *color = [UIColor redColor];
UIFont *font = [UIFont boldSystemFontOfSize:30.f];
[paramText drawAtPoint:CGPointMake(paramTopPoint.x - 40.f, paramTopPoint.y + 60.f) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil]];
}
/**
* 利用路径绘制线条 X
*/
- (void)drawPath{
CGMutablePathRef path = CGPathCreateMutable();
CGRect screenBounds = [[UIScreen mainScreen]bounds];
CGPathMoveToPoint(path, NULL, screenBounds.origin.x, screenBounds.origin.y);
CGPathAddLineToPoint(path, NULL, screenBounds.size.width, screenBounds.size.height);
CGPathMoveToPoint(path, NULL, screenBounds.size.width, screenBounds.origin.y);
CGPathAddLineToPoint(path, NULL, screenBounds.origin.x,screenBounds.size.height);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextAddPath(currentContext, path);
[[UIColor blueColor]setStroke];
CGContextDrawPath(currentContext, kCGPathStroke);
CGPathRelease(path);
}
/**
* 利用可变路径绘制多个矩形
*/
- (void)drawRectRect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle1 = CGRectMake(10.f, 10.f, 200.f, 300.f);
CGRect rectangle2 = CGRectMake(30.f, 30.f, 220.f, 320.f);
CGRect rectangle3 = CGRectMake(50.f, 50.f, 240.f, 340.f);
CGRect rectangle4 = CGRectMake(70.f, 70.f, 260.f, 360.f);
CGRect rectangle5 = CGRectMake(90.f, 90.f, 280.f, 380.f);
// CGPathAddRect(path, NULL, rectangle);
CGRect rects[5] = {rectangle1,rectangle2,rectangle3,rectangle4,rectangle5};
CGPathAddRects(path, NULL, (const CGRect*)&rects,5);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextAddPath(currentContext, path);
[[UIColor colorWithRed:0.2f green:0.6f blue:0.8f alpha:1.0f]setFill];
[[UIColor brownColor]setStroke];
CGContextSetLineWidth(currentContext, 5.0f);
CGContextDrawPath(currentContext, kCGPathFillStroke);
CGPathRelease(path);
}
/**
* 绘制浅蓝色矩形,并带有阴影效果
*/
- (void) drawRectAtTopOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// 保存图形上下文的默认状态
CGContextSaveGState(currentContext);
// 设置矩形阴影
CGContextSetShadowWithColor(currentContext, CGSizeMake(10.f, 10.f), 20.f, [UIColor grayColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGRect firstRect = CGRectMake(55.f, 60.f, 150.f, 150.f);
CGPathAddRect(path, NULL, firstRect);
CGContextAddPath(currentContext, path);
[[UIColor colorWithRed:0.2f green:0.6f blue:0.8f alpha:1.0f] setFill];
CGContextDrawPath(currentContext, kCGPathFill);
CGPathRelease(path);
[self drawRectAtBottomOfScreen];
}
/**
* 绘制的第二个矩形,测试 CGContextRestoreGState 方法的使用效果
*/
- (void)drawRectAtBottomOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// 恢复之前保存的图形上下文状态,这里之前如果不保存状态,那么下面绘制的矩形默认将带有阴影效果
CGContextRestoreGState(currentContext);
CGMutablePathRef secondPath = CGPathCreateMutable();
CGRect secondRect = CGRectMake(150.0f, 320.0f, 100.0f, 100.0f);
CGPathAddRect(secondPath, NULL, secondRect);
CGContextAddPath(currentContext, secondPath);
[[UIColor purpleColor] setFill];
CGContextDrawPath(currentContext, kCGPathFill);
CGPathRelease(secondPath);
}
/**
* 绘制线性渐变,类似彩虹效果
*/
- (void)drawGradient{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *startColor = [UIColor blueColor];
CGFloat *startColorCompents = (CGFloat *)CGColorGetComponents([startColor CGColor]);
UIColor *endColor = [UIColor greenColor];
CGFloat *endColorCompents = (CGFloat *)CGColorGetComponents([endColor CGColor]);
CGFloat colorComponents[8] = {
startColorCompents[0],
startColorCompents[1],
startColorCompents[2],
startColorCompents[3],
endColorCompents[0],
endColorCompents[1],
endColorCompents[2],
endColorCompents[3],
};
CGFloat colorIndices[2] = {
0.0f,
1.0f,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, (const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2);
//释放色彩空间 这里同CGMutablePath一样,需要手动释放其所占的内存空间
CGColorSpaceRelease(colorSpace);
CGRect screenBounds = [[UIScreen mainScreen]bounds];
CGPoint startPoint, endPoint;
startPoint = CGPointMake(0.0f, screenBounds.size.height / 2.0f); // retina 3.5 startPoint = (0,240)
endPoint = CGPointMake(screenBounds.size.width, startPoint.y); // retina 3.5 endPoint = (320,240)
CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, startPoint, endPoint, 0);
//释放渐变空间 这里同CGMutablePath一样,需要手动释放其所占的内存空间
CGGradientRelease(gradient);
}
/**
* 绘制线性渐变2,就改变了线性渐变绘制的起始点和结束点
*/
- (void)drawGradient2{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *startColor = [UIColor orangeColor];
CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]); UIColor *endColor = [UIColor blueColor];
CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);
CGFloat colorComponents[8] = {
startColorComponents[0],
startColorComponents[1],
startColorComponents[2],
startColorComponents[3],
endColorComponents[0],
endColorComponents[1],
endColorComponents[2],
endColorComponents[3],
};
CGFloat colorIndices[2] = {
0.0f,
1.0f,};
CGGradientRef gradient = CGGradientCreateWithColorComponents (colorSpace, (const CGFloat *)&colorComponents,
(const CGFloat *)&colorIndices, 2);
CGColorSpaceRelease(colorSpace);
CGPoint startPoint, endPoint; startPoint = CGPointMake(120, 260); endPoint = CGPointMake(200.0f, 220);
CGContextDrawLinearGradient (currentContext, gradient, startPoint, endPoint,
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(currentContext);
}
/**
* 绘制线性渐变3,包括三个颜色对象
*/
- (void)drawGradient3{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *startColor = [UIColor orangeColor];
CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]);
UIColor *middleColor = [UIColor redColor];
CGFloat *middleColorComponents = (CGFloat *)CGColorGetComponents([middleColor CGColor]);
UIColor *endColor = [UIColor blueColor];
CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);
CGFloat colorComponents[12] = {
startColorComponents[0],
startColorComponents[1],
startColorComponents[2],
startColorComponents[3],
endColorComponents[0],
endColorComponents[1],
endColorComponents[2],
endColorComponents[3],
middleColorComponents[0],
middleColorComponents[1],
middleColorComponents[2],
middleColorComponents[3],
};
CGFloat colorIndices[3] = {
0.0f,
0.3f,
1.0f,};
CGGradientRef gradient = CGGradientCreateWithColorComponents (colorSpace, (const CGFloat *)&colorComponents,
(const CGFloat *)&colorIndices, 3);
CGColorSpaceRelease(colorSpace);
CGPoint startPoint, endPoint; startPoint = CGPointMake(120, 260); endPoint = CGPointMake(200.0f, 220);
CGContextDrawLinearGradient (currentContext, gradient, startPoint, endPoint,
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(currentContext);
}
- (void)affineTransformTest1{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectMake(10.f, 10.f, 200.f, 300.f);
CGAffineTransform transform = CGAffineTransformMakeTranslation(100.f, 0.f);
CGPathAddRect(path, &transform, rectangle);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextAddPath(currentContext, path);
[[UIColor colorWithRed:0.2f green:0.6f blue:0.8f alpha:1.0f]setFill];
[[UIColor brownColor]setStroke];
CGContextSetLineWidth(currentContext, 5.0f);
CGContextDrawPath(currentContext, kCGPathFillStroke);
CGPathRelease(path);
}
- (void)affineTransformTest2{
// Drawing code
/* Create the path first. Just the path handle. */
CGMutablePathRef path = CGPathCreateMutable();
/* Here are our rectangle boundaries */
CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
/* Add the rectangle to the path */
CGPathAddRect(path, NULL, rectangle);
/* Get the handle to the current context */
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Save the state of the context to revert back to how it was at this state, later */
CGContextSaveGState(currentContext);
/* Translate the current transformation matrix to the right by 100 points */
CGContextTranslateCTM(currentContext, 100.0f, 0.0f);
/* Add the path to the context */
CGContextAddPath(currentContext, path);
/* Set the fill color to cornflower blue */
[[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
/* Set the stroke color to brown */
[[UIColor brownColor] setStroke];
/* Set the line width (for the stroke) to 5 */
CGContextSetLineWidth(currentContext, 5.0f);
/* Stroke and fill the path on the context */
CGContextDrawPath(currentContext, kCGPathFillStroke);
/* Dispose of the path */
CGPathRelease(path);
/* Restore the state of the context */
CGContextRestoreGState(currentContext);
}
/**
* 缩放矩形为原来的0.5倍
*/
- (void)scaleTransform{
// Drawing code
/* Create the path first. Just the path handle. */
CGMutablePathRef path = CGPathCreateMutable();
/* Here are our rectangle boundaries */
CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
/* Add the rectangle to the path */
CGPathAddRect(path, NULL, rectangle);
/* Get the handle to the current context */
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Scale everything drawn on the current graphics context to half itssize */
CGContextRotateCTM(currentContext,(-70.0f * M_PI) / 180.0f);
/* Add the path to the context */
CGContextAddPath(currentContext, path);
/* Set the fill color to cornflower blue */
[[UIColor colorWithRed:0.20f green:0.60f blue:0.80 alpha:1.0f] setFill];
/* Set the stroke color to brown */
[[UIColor brownColor] setStroke];
/* Set the line width (for the stroke) to 5 */
CGContextSetLineWidth(currentContext, 5.0f);
/* Stroke and fill the path on the context */
CGContextDrawPath(currentContext, kCGPathFillStroke);
/* Dispose of the path */
CGPathRelease(path);
/* Start from top left corner */
UIImage *img = [UIImage imageNamed:@"20140901110652984"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self addSubview:imgView];
[imgView setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
imgView.transform = CGAffineTransformIdentity;
[UIView beginAnimations:@"xcodeImageViewAnimation" context:nil];
[UIView setAnimationDuration:2.0f];
/* Receive animation delegates */
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: @selector(imageViewDidStop:finished:context:)];
/* End at the bottom right corner */
// [imgView setFrame: CGRectMake(200.0f, 350.0f, 100.f, 100.f)];
imgView.center = CGPointMake(200.f, 350.f);
imgView.transform = CGAffineTransformMakeRotation(180 * M_PI / 180.f);
[UIView commitAnimations];
}
- (void)imageViewDidStop:(NSString *) paramAnimationID finished:(NSNumber *)paramFinished context:(void *)paramContext {
NSLog(@"Animation finished.");
NSLog(@"Animation ID = %@", paramAnimationID);
// UIImageView *contextImageView = (__bridge UIImageView *)paramContext;
// NSLog(@"Image View = %@", contextImageView);
}
IOS 图形和简单的动画处理代码片段
最新推荐文章于 2018-12-01 10:52:31 发布