0
我在根据触摸位置定位图像时遇到问题,但仅限于圆形。根据触摸位置在屏幕上放置图像,将图像的位置限制为一个圆形
它适用于大多数情况,但如果角度(从触摸位置到所需位置)小于0,则会将图像定位在圆圈的错误一侧。
也许这是我做错了一些数学。
总之,这里的代码:
float newHeight, newWidth, centerPointX, centerPointY;
newHeight = -(invertedY.y - (view.frame.origin.y+view.frame.size.height/2));
newWidth = -(invertedY.x - (view.frame.origin.x+view.frame.size.width/2));
float tangent = newHeight/newWidth;
float calculatedAngle = atanf(tangent);
float s, c, d, fX, fY;
d = view.frame.size.width/2+30;
if (calculatedAngle < 0) {
s = sinf(calculatedAngle) * d;
c = cosf(calculatedAngle) * d;
} else {
s = -sinf(calculatedAngle) * d;
c = -cosf(calculatedAngle) * d;
}
fX = view.center.x + c;
fY = view.center.y + s;
[delegate setPoint:CGPointMake(fX, fY)];
NSLog(@"angle = %.2f", calculatedAngle);
任何帮助表示赞赏。
它完美的工作,谢谢。 –