使用CALayer绘制线 [英] Draw a line with a CALayer
问题描述
我想使用CALayer在两点之间画一条线。这是我的代码:
//将CALayer定位为父节点及其子节点之间的线。
- (void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB {
NSLog([NSString stringWithFormat:@Coordinates: \\ n Ax:%f Ay:%f Bx:%f By:%f,pointA.x,pointA.y,pointB.x,pointB.y]
//找到行的长度:
CGFloat length = sqrt((pointA.x - pointB.x)*(pointA.x - pointB.x)+(pointA.y - pointB.y)*(pointA.y - pointB.y));
layer.frame = CGRectMake(0,0,1,length);
//计算并设置图层的中心:
CGPoint center = CGPointMake(pointA.x + pointB.x)/ 2,(pointA.y + pointB.y)/ 2) ;
layer.position = center;
//计算线的角度并设置图层的变换以匹配它。
CGFloat angle = atan2f(pointB.y - pointA.y,pointB.x - pointA.x);
layer.transform = CATransform3DMakeRotation(angle,0,0,1);
}
我知道长度正确计算,确定中心也是。当我运行它显示线是正确的长度,通过两点之间的中心点,但不能正确旋转。起初我以为这条线绕着错误的锚点旋转,所以我做了: layer.anchorPoint = center;
,但是这个代码没有显示任何行屏幕。
<$ p $ p>
- (void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB
{
CAShapeLayer * line = [CAShapeLayer layer];
UIBezierPath * linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:pointA];
[linePath addLineToPoint:pointB];
line.path = linePath.CGPath;
line.fillColor = nil;
line.opacity = 1.0;
line.strokeColor = [UIColor redColor] .CGColor;
[layer addSublayer:line];
}
I'm trying to draw a line between two points using a CALayer. Here is my code:
//positions a CALayer to be a line between a parent node and its subnodes.
-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB{
NSLog([NSString stringWithFormat:@"Coordinates: \n Ax: %f Ay: %f Bx: %f By: %f", pointA.x,pointA.y,pointB.x,pointB.y]);
//find the length of the line:
CGFloat length = sqrt((pointA.x - pointB.x) * (pointA.x - pointB.x) + (pointA.y - pointB.y) * (pointA.y - pointB.y));
layer.frame = CGRectMake(0, 0, 1, length);
//calculate and set the layer's center:
CGPoint center = CGPointMake((pointA.x+pointB.x)/2, (pointA.y+pointB.y)/2);
layer.position = center;
//calculate the angle of the line and set the layer's transform to match it.
CGFloat angle = atan2f(pointB.y - pointA.y, pointB.x - pointA.x);
layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
}
I know that the length is being calculated correctly, and I'm am pretty sure that the center is also. When I run it lines are displayed that are the right length and that pass through the center point between the two points, but are not rotated correctly. At first I thought that the line was being rotated around the wrong anchor point, so I did: layer.anchorPoint = center;
, but this code fails to show any lines on the screen. What am I doing wrong
Try This...
-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB
{
CAShapeLayer *line = [CAShapeLayer layer];
UIBezierPath *linePath=[UIBezierPath bezierPath];
[linePath moveToPoint: pointA];
[linePath addLineToPoint:pointB];
line.path=linePath.CGPath;
line.fillColor = nil;
line.opacity = 1.0;
line.strokeColor = [UIColor redColor].CGColor;
[layer addSublayer:line];
}
这篇关于使用CALayer绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!