转自:http://www.maxiaoguo.com/clothes/252.html
基本图形的绘制 包括: 代码画线,画文字 图片 裁剪 重绘 简单动画
当自定义view的时候 系统会自动调用drawRect 方法
画线
-
- (void)drawRect:(CGRect)rect
-
{
-
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
-
CGContextSetLineWidth(ctx, 10);
-
-
-
CGContextSetLineCap(ctx, kCGLineCapRound);
-
-
-
CGContextSetLineJoin(ctx, kCGLineJoinRound);
-
-
-
-
CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
-
-
CGContextMoveToPoint(ctx, 10, 10);
-
-
CGContextAddLineToPoint(ctx, 100, 100);
-
-
-
CGContextStrokePath(ctx);
-
-
-
-
-
CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);
-
-
CGContextMoveToPoint(ctx, 200, 190);
-
-
CGContextAddLineToPoint(ctx, 150, 40);
-
CGContextAddLineToPoint(ctx, 120, 60);
-
-
-
-
CGContextStrokePath(ctx);
-
}
画圆弧
-
-
-
-
void drawArc()
-
{
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
-
-
-
-
-
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
-
-
-
-
CGContextFillPath(ctx);
-
}
画圆
-
-
-
-
void drawCircle()
-
{
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));
-
-
CGContextSetLineWidth(ctx, 10);
-
-
-
CGContextStrokePath(ctx);
-
}
画矩形
-
-
-
-
void draw4Rect()
-
{
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));
-
-
-
-
-
[[UIColor whiteColor] set];
-
-
-
-
-
CGContextFillPath(ctx);
-
}
画三角形
-
-
-
-
void drawTriangle()
-
{
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
CGContextMoveToPoint(ctx, 0, 0);
-
CGContextAddLineToPoint(ctx, 100, 100);
-
CGContextAddLineToPoint(ctx, 150, 80);
-
-
CGContextClosePath(ctx);
-
-
-
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
-
-
-
CGContextStrokePath(ctx);
-
}
画文字
-
-
-
-
void drawText()
-
{
-
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
CGRect cubeRect = CGRectMake(50, 50, 100, 100);
-
CGContextAddRect(ctx, cubeRect);
-
-
CGContextFillPath(ctx);
-
-
-
-
-
NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";
-
-
-
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
-
-
-
attrs[NSForegroundColorAttributeName] = [UIColor redColor];
-
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];
-
[str drawInRect:cubeRect withAttributes:attrs];
-
}
画图片
-
void drawImage()
-
{
-
-
UIImage *image = [UIImage imageNamed:@"me"];
-
-
-
-
-
[image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];
-
-
-
NSString *str = @"为xxx所画";
-
[str drawInRect:CGRectMake(0, 180, 100, 30) withAttributes:nil];
-
}
在绘制的时候 当设置了ctx 的颜色的时候 再绘制其他的图时,颜色需要重置,很麻烦,解决方法是重置 ctx 如下
-
-
CGContextSaveGState(ctx);
-
-
-
CGContextSetLineWidth(ctx, 10);
-
[[UIColor redColor] set];
-
CGContextSetLineCap(ctx, kCGLineCapRound);
-
-
-
CGContextMoveToPoint(ctx, 50, 50);
-
CGContextAddLineToPoint(ctx, 120, 190);
-
-
CGContextStrokePath(ctx);
-
-
-
CGContextRestoreGState(ctx);
整个ctx 的旋转 移动
-
CGContextRotateCTM(ctx, M_PI_4 * 0.3);
-
CGContextScaleCTM(ctx, 0.5, 0.5);
-
CGContextTranslateCTM(ctx, 0, 150);
图片的裁剪 思路是 裁剪ctx的显示区域
-
- (void)drawRect:(CGRect)rect
-
{
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
CGContextSaveGState(ctx);
-
-
-
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
-
-
CGContextClip(ctx);
-
CGContextFillPath(ctx);
-
-
-
UIImage *image = [UIImage imageNamed:@"me"];
-
[image drawAtPoint:CGPointMake(100, 100)];
-
-
CGContextRestoreGState(ctx);
-
-
CGContextAddRect(ctx, CGRectMake(0, 0, 50, 50));
-
CGContextFillPath(ctx);
-
}
动画
CADisplayLink 是一个定时器,特点 刷新频率高, setNeedsDisplay方法起重新绘制的作用
-
- (void)awakeFromNib
-
{
-
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
-
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
-
-
-
}
-
-
- (void)drawRect:(CGRect)rect
-
{
-
self.snowY+=5;
-
-
if (self.snowY >= rect.size.height) {
-
self.snowY = -100;
-
}
-
-
UIImage *image = [UIImage imageNamed:@"snow.jpg"];
-
[image drawAtPoint:CGPointMake(0, self.snowY)];
-
}
通过路径画图
-
- (void)drawRect:(CGRect)rect
-
{
-
CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
CGMutablePathRef linePath = CGPathCreateMutable();
-
-
-
-
-
CGPathMoveToPoint(linePath, NULL, 0, 0);
-
CGPathAddLineToPoint(linePath, NULL, 100, 100);
-
-
-
CGContextAddPath(ctx, linePath);
-
-
CGMutablePathRef circlePath = CGPathCreateMutable();
-
CGPathAddArc(circlePath, NULL, 150, 150, 50, 0, M_PI * 2, 0);
-
CGContextAddPath(ctx, circlePath);
-
-
-
CGContextStrokePath(ctx);
-
-
-
CGPathRelease(linePath);
-
CGPathRelease(circlePath);
-
-
-
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
-
CGColorSpaceRelease(cs);
-
-
-
-
-
}
iOS绘图教程
原文:http://blog.csdn.net/nvermore_/article/details/51315829