一、畫圖形(這些都是要繼承uiview 重寫(void)drawRect:(CGRect)rect 方法,后面會說在viewcontroller中如何畫)
1.圓形
// 圓角矩形
// UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];
// 圓弧
// Center:圓心
// startAngle:弧度
// clockwise:YES:順時針 NO:逆時針
// 扇形
CGPoint center = CGPointMake(125, 125);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
// 添加一根線到圓心
[path addLineToPoint:center];
// 封閉路徑,關(guān)閉路徑:從路徑的終點(diǎn)到起點(diǎn)
// [path closePath];
// 就是畫一個圓弧
// [path stroke];
// 填充:必須是一個完整的封閉路徑,默認(rèn)就會自動關(guān)閉路徑
// 畫一個圓餅
[path fill];
2.柱狀圖
w = rect.size.width / (2 * arr.count - 1);
x = 2 * w * i;
h = [arr[i] floatValue] / 100.0 * rect.size.height;
y = rect.size.height - h;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
[[self colorRandom] set];
[path fill];
3.圖片和文字繪制到特定圖形上面
// 超出裁剪區(qū)域的內(nèi)容全部裁剪掉
// 注意:裁剪必須放在繪制之前
// UIRectClip(CGRectMake(0, 0, 50, 50));
UIImage *image = [UIImage imageNamed:@"001"];
// 默認(rèn)繪制的內(nèi)容尺寸跟圖片尺寸一樣大
// [image drawAtPoint:CGPointZero];
// 在哪個范圍中繪制圖片,就是填充 會拉伸
// [image drawInRect:rect];
// 平鋪整個畫布
[image drawAsPatternInRect:rect];
文字
// 繪制文字
NSString *str = @"asfdsfsdf";
// 文字的起點(diǎn)
// Attributes:文本屬性
NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
// 設(shè)置文字顏色
textDict[NSForegroundColorAttributeName] = [UIColor redColor];
// 設(shè)置文字字體
textDict[NSFontAttributeName] = [UIFont systemFontOfSize:30];
// 設(shè)置文字的空心顏色和寬度
textDict[NSStrokeWidthAttributeName] = @3;
textDict[NSStrokeColorAttributeName] = [UIColor yellowColor];
// 創(chuàng)建陰影對象
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor greenColor];
shadow.shadowOffset = CGSizeMake(4, 4);
shadow.shadowBlurRadius = 3;
textDict[NSShadowAttributeName] = shadow;
// 富文本:給普通的文字添加顏色,字體大小
[str drawAtPoint:CGPointZero withAttributes:textDict];
// 繪制在某個區(qū)域中,文字長會換行
// [str drawInRect:rect withAttributes:textDict];
4.圖形的一些操作
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
[[UIColor redColor] set];
// 上下文矩陣操作
// 注意:矩陣操作必須要在添加路徑之前
// 平移
CGContextTranslateCTM(ctx, 100, 50);
// 縮放
CGContextScaleCTM(ctx, 0.5, 0.5);
// 旋轉(zhuǎn)
CGContextRotateCTM(ctx, M_PI_4);
// 3.把路徑添加上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColor redColor] set];
// 4.渲染上下文
CGContextFillPath(ctx);
demo地址
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。