/*
提示:如果是畫線,那麼就創建一條路徑(path)用來保存畫線的繪圖信息,如果又要重新畫一個圓,那麼就可以創建一條新的路徑來專門保存畫圓的繪圖信息。
凡通過quarzt2d中帶有creat/copy/retain方法創建出來的值都必須手動的釋放
有兩種方法可以釋放前面創建的路徑:
(1)CGPathRelease(path);
(2)CFRelease(path);
說明:CFRelease屬於更底層的cocafoundation框架
*/
//畫矩形
- (void)drawRectangle:(CGRect)rect {
//得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//定義一個畫圖的軌跡
CGMutablePathRef pathRef = [self pathwithFrame:rect withRadius:0];
//添加到上下文中
CGContextAddPath(context, pathRef);
//畫圖
CGContextDrawPath(context, kCGPathFillStroke);
}
//畫直線
- (void)drawLineFrom:(CGPoint)startPoint to:(CGPoint)endPoint {
//得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//畫圖
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
}
//圓形
-(void)drawCircleWithCenter:(CGPoint)center
radius:(float)radius {
//得到上下文
CGContextRef content = UIGraphicsGetCurrentContext();
//創建一條畫圖路徑
//注意:但凡通過Quartz2D中帶有creat/copy/retain方法創建出來的值都必須要釋放
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, &CGAffineTransformIdentity, center.x, center.y,radius, -PI/2, radius*2*PI-PI/2, NO);
//清楚路徑
CGPathCloseSubpath(path);
//保存到圖文上下文
CGContextAddPath(content, path);
//渲染
CGContextDrawPath(content, kCGPathFillStroke);
//釋放路徑
CGPathRelease(path);
//把園的繪圖信息添加到路徑裏
// CGPathAddEllipseInRect(path, nil, <#CGRect rect#>)
}
//曲線
-(void)drawCurveFrom:(CGPoint)startPoint
to:(CGPoint)endPoint
controlPoint1:(CGPoint)controlPoint1
controlPoint2:(CGPoint)controlPoint2 {
//得到圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//設定一個點
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
//添加一個曲線
CGContextAddCurveToPoint(context, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
//渲染曲線
CGContextDrawPath(context, kCGPathStroke);
}
//弧線
-(void)drawArcFromCenter:(CGPoint)center
radius:(float)radius
startAngle:(float)startAngle
endAngle:(float)endAngle
clockwise:(BOOL)clockwise
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context,
center.x,
center.y,
radius,
startAngle,
endAngle,
clockwise?0:1);
CGContextStrokePath(context);
}
-(void)drawLines:(NSArray *)pointArray
{
NSAssert(pointArray.count>=2,@"數組長度必須大於等於2");
NSAssert([[pointArray[0] class] isSubclassOfClass:[NSValue class]], @"數組成員必須是CGPoint組成的NSValue");
CGContextRef context = UIGraphicsGetCurrentContext();
NSValue *startPointValue = pointArray[0];
CGPoint startPoint = [startPointValue CGPointValue];
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
for(int i = 1;i<pointArray.count;i++)
{
NSAssert([[pointArray[i] class] isSubclassOfClass:[NSValue class]], @"數組成員必須是CGPoint組成的NSValue");
NSValue *pointValue = pointArray[i];
CGPoint point = [pointValue CGPointValue];
CGContextAddLineToPoint(context, point.x,point.y);
}
CGContextStrokePath(context);
}
//動態折線圖
- (void)drawLInes:(CGPoint)onpoint points:(NSArray *)points{
NSAssert(points.count>=2,@"數組長度必須大於等於2");
NSAssert([[points[0] class] isSubclassOfClass:[NSValue class]], @"數組成員必須是CGPoint組成的NSValue");
[self setNeedsDisplay];
[self drawLinean:CGPointMake(onpoint.x, 100) to:CGPointMake(onpoint.x+300, 100)];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:onpoint];
for (int i= 0; i < points.count; i++) {
NSAssert([[points[i] class] isSubclassOfClass:[NSValue class]], @"數組成員必須是CGPoint組成的NSValue");
NSValue *point = points[i];
[path addLineToPoint:[point CGPointValue]];
}
NSLog(@"開始%f 結束 %@ ",onpoint.y,[points lastObject]);
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor redColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 1.0f;
pathLayer.lineJoin = kCALineJoinRound;
[self.layer addSublayer:pathLayer];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 3.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:2.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}
#pragma ---------func
- (void)drawLinean:(CGPoint)startpoint to:(CGPoint)point {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startpoint];
[path addLineToPoint:point];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor colorWithRed:80.f/255.f
green:150.f/255.f
blue:225.f/255.f
alpha:1] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 1.0f;
pathLayer.lineJoin = kCALineJoinRound;
[self.layer addSublayer:pathLayer];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 3.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:2.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}
- (CGMutablePathRef)pathwithFrame:(CGRect)frame withRadius:(CGFloat)radius {
//設置矩形的四個點
CGPoint x1,x2,x3,x4;
x1 = frame.origin;
x2 = CGPointMake(frame.origin.x+frame.size.width, frame.origin.y);
x3 = CGPointMake(frame.origin.x+frame.size.width, frame.origin.y+frame.size.height);
x4 = CGPointMake(frame.origin.x, frame.origin.y+frame.size.height);
CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, &CGAffineTransformIdentity,x1.x,x1.y);
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, x2.x,x2.y);
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, x3.x,x3.y);
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, x4.x,x4.y);
CGPathCloseSubpath(pathRef);
return pathRef;
}