//當時圖顯示的時候調用默認調用一次
- (void)drawRect:(CGRect)rect {}
餅狀圖
-(void)drawPieChart{
/**
*
NSArray *array = @[@25,@25, @50];
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGPoint centerP = CGPointMake(125, 125);
CGFloat radius = 120;
CGFloat startA = 0;
CGFloat angle = 0;
CGFloat endA = 0;
for (NSNumber *num in array) {
startA = endA;
angle = num.intValue / 100.0 * M_PI *2;
endA = startA + angle;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path addLineToPoint:centerP];
// 添加上下文
CGContextAddPath(contextRef, path.CGPath);
[[UIColor randomColor] set];
// 渲染 不渲染的話後面的設置比如顏色會被覆蓋掉
CGContextFillPath(contextRef);
}
*/
}
-(void)drawCircle{
CGContextRef contextRef =UIGraphicsGetCurrentContext();
CGFloat radius =60;
CGPoint centerP =CGPointMake(50,50);
UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:centerPradius:radius startAngle:0endAngle:M_PIclockwise:NO];
CGContextAddPath(contextRef, path.CGPath);
// CGContextStrokePath(contextRef);
CGContextFillPath(contextRef);
}
-(void)dreaOval{
CGContextRef contextRef =UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(10,10,200,100)];
CGContextAddPath(contextRef, path.CGPath);
CGContextStrokePath(contextRef);
}
-(void)draeRect{
CGContextRef contextRef =UIGraphicsGetCurrentContext();
// 繪製正方形
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)];
// 設圓角置正方形的
UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(10,10,100,100)cornerRadius:20];
CGContextAddPath(contextRef, path.CGPath);
CGContextStrokePath(contextRef);
}
-(void)drawTrigon{
CGContextRef contextRef =UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint startP =CGPointMake(10,10);
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(60,60)];
[path addLineToPoint:CGPointMake(120,10)];
// 從路徑的終點連接到起點
// [path addLineToPoint:startP];
// 關閉路徑默認從路徑的終點連接到起點
[path closePath];
CGContextAddPath(contextRef, path.CGPath);
[[UIColorpurpleColor]setFill];
[[UIColorblackColor]setStroke];
CGContextSetLineWidth(contextRef,10);
// 不填充
// CGContextStrokePath(contextRef);
// 將劃線的閉合空間填充顏色
// CGContextFillPath(contextRef);
// 填充與線的顏色都顯示模式 kCGPathFillStroke
CGContextDrawPath(contextRef,kCGPathFillStroke);
}
-(void)drawQu{
CGContextRef contextRet =UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint startPoint = CGPointMake(10,100);
CGPoint endPoint =CGPointMake(100,100);
CGPoint topPoint =CGPointMake(50,10);
[path moveToPoint:startPoint];
[path addQuadCurveToPoint:endPointcontrolPoint:topPoint];
CGContextAddPath(contextRet, path.CGPath);
CGContextStrokePath(contextRet);
}
-(void)drawLine2{
// 上下文
CGContextRef contextRef =UIGraphicsGetCurrentContext();
// 拼接路徑
UIBezierPath *path = [UIBezierPathbezierPath];
// 設置起點
[path moveToPoint:CGPointMake(10,10)];
[path addLineToPoint:CGPointMake(30,30)];
// 重新新建一條路徑顯示
// [path moveToPoint:CGPointMake(20, 20)];
// [path addLineToPoint:CGPointMake(40, 50)];
// 路徑添加到上下文
CGContextAddPath(contextRef, path.CGPath);
// 設置線段寬度
CGContextSetLineWidth(contextRef,10);
// 設置線段圓角
CGContextSetLineCap(contextRef,kCGLineCapRound);
// 設置線段顏色
// CGContextSetRGBFillColor(contextRef, 1,0,0,1);
[[UIColorgrayColor]set];
// 渲染上下文
CGContextStrokePath(contextRef);
}
-(void)drawLine{
//獲取上下文
CGContextRef ref =UIGraphicsGetCurrentContext();
//繪製路徑
UIBezierPath *path = [UIBezierPathbezierPath];
//起始點
[path moveToPoint:CGPointMake(10,10)];
// 添加一條線到某點
[path addLineToPoint:CGPointMake(200,200)];
[path addLineToPoint:CGPointMake(300,10)];
// 把路徑添加到上下文 path.CGPath直接把UIKit的路徑轉化爲CoreGraphics
CGContextAddPath(ref, path.CGPath);
// 把上下文渲染到視圖
CGContextStrokePath(ref);
}
繪製圖片以及文字
-(void)drawImage{
// UIImage *image = [UIImage imageNamed:@"123"];
// [image drawAtPoint:CGPointZero];
// [image drawAtPoint:rect];
// // 平鋪
// [image drawAsPatternInRect:rect];
//
}
-(void)drawText{
NSString *text=@"1234zhangsdfjsld;fjs轉身離開的減肥啦上;GDSF";
CGRect rectFrame =CGRectMake(0,0, 200,200);
NSDictionary *dicty =@{
NSFontAttributeName :[UIFontsystemFontOfSize:20],
NSForegroundColorAttributeName:[UIColorredColor],
// NSStrokeWidthAttributeName:@5
};
// 最自動換行
[text drawInRect:rectFramewithAttributes:dicty];
// 不會自動換行
[text drawAtPoint:CGPointZerowithAttributes:dicty];
}
圖形繪製的一些矩陣的操作
// 獲取上下文
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// 平移上下文 注意:路徑一定上下文操作矩陣之後
CGContextTranslateCTM(contextRef, 50, 100);
// 旋轉操作
CGContextRotateCTM(contextRef, M_PI_4);
// 縮放操作
CGContextScaleCTM(contextRef, 0.5, 0.3);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-50, -100, 150, 200)];
CGContextAddPath(contextRef, path.CGPath);
CGContextFillPath(contextRef);