【发布时间】:2023-04-08 22:36:01
【问题描述】:
我正在尝试使用 CALayer 在两点之间画一条线。这是我的代码:
//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);
}
我知道长度计算正确,我很确定中心也是。当我运行它时,显示的线条长度正确,并且穿过两点之间的中心点,但没有正确旋转。起初我以为线条围绕错误的锚点旋转,所以我做了:layer.anchorPoint = center;,但这段代码无法在屏幕上显示任何线条。我做错了什么
【问题讨论】:
-
锚点在单位坐标空间中(x和y都从0到1)。这意味着无论图层的大小和纵横比如何,居中的锚点都是 (0.5, 0.5)。
-
好的,所以
layer.anchorPoint = center;将锚点设置为远离屏幕的某个位置,并且旋转将超出屏幕的约束范围......它不显示是有道理的。你知道问题可能是什么吗?
标签: objective-c cocoa-touch calayer transformation quartz-core