iOS:相对于可通过触摸调整的点旋转一条线
老兄,我一直在和这个 参考上面的图片,我有一条固定在特定点上的线。在这条线的另一端,我有另一个不固定的点,可以拖动到屏幕上的任何位置。我需要线能够跟随那个点,并且在不改变长度的情况下相对于它的位置旋转 我知道所有这些都可以很容易地完成,而不需要固定长度的线路 您只需在iOS:相对于可通过触摸调整的点旋转一条线,ios,core-graphics,quartz-2d,cgaffinetransform,uibezierpath,Ios,Core Graphics,Quartz 2d,Cgaffinetransform,Uibezierpath,老兄,我一直在和这个 参考上面的图片,我有一条固定在特定点上的线。在这条线的另一端,我有另一个不固定的点,可以拖动到屏幕上的任何位置。我需要线能够跟随那个点,并且在不改变长度的情况下相对于它的位置旋转 我知道所有这些都可以很容易地完成,而不需要固定长度的线路 您只需在drawRect:中绘制UIBezierPath,如下所示: UIBezierPath *bezPath = [UIBezierPath bezierPath]; [bezPath moveToPoint:fixedPointSta
drawRect:
中绘制UIBezierPath
,如下所示:
UIBezierPath *bezPath = [UIBezierPath bezierPath];
[bezPath moveToPoint:fixedPointStart];
[bezPath addLineToPoint:draggedPointEnd];
[bezPath stroke];
然后在您的UIView中调用-(void)touchesMoved:(NSSet*)toucheevent:(UIEvent*)event
中的[self-setNeedsDisplay]
,传递被拖动点的新坐标并重新绘制
但正如我提到的,我需要一条固定长度的线
还有一件事(不确定这是否会产生影响),这条线实际上是不规则形状的更大的UIBezierPath
的一部分。图中显示的“特定固定点”是这条线与较大的不规则路径的连接点。这条线是作为相同的UIBezierPath
的一部分绘制的,您将在下面的代码中看到
这就是我现在拥有的:
// I AM CREATING THE FIXED POINT HERE THAT IS BEING RETURNED BY A CUSTOM METHOD
WHICH FINDS THE POINT OF ATTACHEMENT TO THE LARGER IRREGULAR SHAPED UIBEZIERPATH.
// THE DRAGGABLE POINTS ARE ACTUALLY UIVIEW'S AND I'M USING THEIR ORIGINS.
CGPoint fixedPoint = [aPath findCurveBezierPathPointInBetweenTheStartPoint:point1.frame.origin
theEndPoint:topCurvePoint.frame.origin
withFirstControlPoint:topCurveFirstControlPoint.frame.origin
andSecondControlPoint:topCurveSecondControlPoint.frame.origin atPercentage:0.3];
// This is the fixed point
CGPoint startPoint = fixedPoint;
// This is the point that is going to be able to be dragged
CGPoint endPoint = topCurveFirstControlPoint.frame.origin;
// I am finding the angle in between these two points to pass in an CGAffineTransform for the rotation.
float angle = [self getRotatingAngle:startPoint secondPoint:endPoint];
NSLog(@"Angle in Radians: %f",angle);
// I am creating the transform by passing in the angle and doing the necessary translations for the rotation to occur on the fixed point.
CGAffineTransform transform = CGAffineTransformMakeTranslation(fixedPoint.x, fixedPoint.y);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformTranslate(transform,-fixedPoint.x,-fixedPoint.y);
// I am setting up to draw the line of fixed length and applying the transform and
[aPath moveToPoint:startPoint];
[aPath addLineToPoint:CGPointMake(fixedPoint.x, fixedPoint.y - 60)];
[aPath applyTransform:transform];
// I then continue to draw the rest of the BezierPath and stroke it at the end.
[aPath moveToPoint:point1.frame.origin];
[aPath addCurveToPoint:topCurvePoint.frame.origin controlPoint1:topCurveFirstControlPoint.frame.origin controlPoint2:topCurveSecondControlPoint.frame.origin];
所以我的问题是:
当可拖动点找到新位置时,每次调用drawRect:
时,如何找到正确的角度来变换固定线
我用来找到固定点和拖动点之间的角度的方法为我提供了正确的角度,因为我已经测试过了。但是,我不知道如何应用这两个点之间的角度,使线相对于可拖动点正确旋转
编辑:为了更好地解释我到底想完成什么而添加 你为什么要改变它?它的几何结构很简单。您可以找到中心和当前点之间的角度。因为长度相等,所以你的路径基本上是一个圆。所以,用圆心和角度找到圆上的点。 假设拖动触摸点后的点是currentPoint。 要查找角度,请使用:
CGFloat angle = atan2f(currentPoint.y - centre.y, currentPoint.x - centre.x);
现在在圆上找到与上述角度对应的点:
finalPoint.x = centre.x + length * cosf(angle);
finalPoint.y = centre.y + length * sinf(angle);
其中长度是您的固定长度。现在,您可以使用quartz-2d在中心点和最终点之间绘制一条线
注意:有时角度可能变为负值。因此,在您的实现中,您可能希望将其转换为等效的+ve角度
if (angle < 0)
angle += 2 * M_PI;
if(角度<0)
角度+=2*M_π;
毕达哥拉斯是你的朋友。你能详细说明一下吗?我知道两点之间的角度,我很难将其应用于旋转。