context画线制作的手表
画线
画直线
//获取view上下文所有画线方法的基础
CGContextRef context =UIGraphicsGetCurrentContext();
//改变线条颜色
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
//设置路径颜色
CGContextSetRGBStrokeColor(context,0.5,0.5, 0.5,1);
//设置路径起始坐标
CGContextMoveToPoint(context,0,0);
//设置路径的终点坐标
CGContextAddLineToPoint(context,100,100);
//设置路径宽度
CGContextSetLineWidth(context,2.0);
//渲染路径
CGContextStrokePath(context);
//下面两句成对使用用于保存和释放上次绘图的状态 每次绘制最好是用这两句话包起来 这样设置的颜色和线条宽度不会变成通用设置了
//储存上下文的状态
CGContextSaveGState(context);
//释放保存的上下文
CGContextRestoreGState(context);
绘制文字
//创建一个字体对象并设置大小此方法无需引用上下文
UIFont *font = [UIFontboldSystemFontOfSize:24.0];
//创建一个字典设置字体大小和文字颜色
NSDictionary * dictionary = [[NSDictionaryalloc]initWithObjectsAndKeys:font,NSFontAttributeName,[UIColorredColor],NSForegroundColorAttributeName,nil];
//将文本内容渲染到view上
[@"123\n456"drawInRect:CGRectMake(20,40,320, 480)withAttributes:dictionary];
绘制矩形
//设置路径颜色
CGContextSetRGBStrokeColor(context,0.5,0.5, 0.5,1);
//绘制一个空心矩形的路径
CGContextAddRect(context,CGRectMake(0,0,50,50));
//设置无边框路径的填充颜色
CGContextSetRGBFillColor(context,1,1,0,1);
//绘制一个无边框的路径
CGContextFillRect(context,CGRectMake(50,50,50, 50));
绘制椭圆
//设置一个空心圆形的路径
CGContextAddEllipseInRect(context,CGRectMake(100,100,50, 50));
//设置一个实心圆
CGContextFillEllipseInRect(context,CGRectMake(150,150,60, 50));
绘制贝兹曲线
//设置路径起始坐标
CGContextMoveToPoint(context,0,0);
//绘制3次曲线
CGContextAddCurveToPoint(context, 200, 100, 200, 200,200,100);
//绘制2次贝兹曲线
CGContextAddQuadCurveToPoint(context,150,10, 300,220);
//绘制弧线,带角度
100,200, 300,200, 100);
绘制虚线
//设置虚线样式
CGFloat aa[] = {5,5,2,2};
//设置虚线 0表示从数组的第几个开始,4表示数组的长度 数组内表示绘点得样式为画5虚5画2虚2
CGContextSetLineDash(context, 0, aa, 4);
绘制渐变颜色
//绘制渐变颜色 这个方法还没搞明白 慎用
CGContextClip(context);
CGColorSpaceRef rgb =CGColorSpaceCreateDeviceRGB();
CGFloat
{
204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,
29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,
0.0 / 255.0, 50.0 / 255.0, 126.0 / 255.0, 1.00,
};
CGGradientRef gradient =CGGradientCreateWithColorComponents
NULL,sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);
0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),kCGGradientDrawsBeforeStartLocation);
绘制图片
1:无上下文绘制
//获取图片的地址返回一个NSString类型地址 无需上下文
NSString * imgPath = [[NSBundlemainBundle]pathForResource:@"地球"ofType:@"jpg"];
//获取地址中的文件
UIImage * myImg = [[UIImage alloc] initWithContentsOfFile:imgPath];
//直接使用下面的这个效果一样
UIImage * myImage = [UIImage imageNamed:@"地球.jpg"];
//将图片渲染到view上
drawInRect:CGRectMake(0,0,50, 50)];
2:上下文绘制
//获取view的上下文 此方法绘出的图像是倒立的
CGContextRef context =UIGraphicsGetCurrentContext();
//获取图片
UIImage * img = [UIImage imageNamed:@"地球.jpg"];
//获取位图
CGImageRef image = img.CGImage;
//保存上下文的状态压栈
CGContextSaveGState(context);
//为视图设置大小
CGRect touchRect = CGRectMake(0,0, img.size.width, img.size.height);
//绘图
CGContextDrawImage(context, touchRect, image);
//出栈
CGContextRestoreGState(context);
3:旋转
//获取图片
UIImage * img = [UIImage imageNamed:@"地球.png"];
//获取位图
CGImageRef image = img.CGImage;
//储存上下文的状态
CGContextSaveGState(context);
//设置旋转中心
CGContextTranslateCTM(context,160,240);
//设置绘图的大小和坐标 XY以中心为准
CGRect touchRect = CGRectMake(10,10,50,50);
//旋转 这个方法写在设置中心和大小的前面和后面效果完全不同的
CGContextRotateCTM(context,angle * M_PI/180);
//绘制图片
CGContextDrawImage(context, touchRect, image);
//释放保存的上下文
CGContextRestoreGState(context);