您的位置:首页 > 移动开发 > IOS开发

iOS 划线总结

2016-04-26 17:41 441 查看
/*

提示:如果是画线,那么就创建一条路径(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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: