根据触摸位置在屏幕上放置图像,将图像的位置限制在一个圆圈内

发布于 2024-12-08 16:37:20 字数 876 浏览 1 评论 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);

任何帮助表示赞赏。

I have a problem regarding positioning an image according to the touches location, however limited to a circle.

It works for the most part, but if the angle (from the touches location to the desired location) is less than 0, it positions the image on the wrong side of the circle.

Perhaps it's some maths that I've done wrong.

Anyway, here's the code:

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);

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-12-15 16:37:20

我认为将位置限制在圆内的最佳方法是计算从中心到触摸位置的矢量。计算向量长度,然后将其除以该长度,以便对其进行归一化。然后将归一化向量乘以圆的半径,最后将该向量添加到中心以计算新位置。

CGPoint touch, center;
CGPoint vector = CGPointMake(touch.x-center.x, touch.y-center.y);

float length = sqrtf(vector.x*vector.x + vector.y*vector.y); 
// Normalize and multiply by radius (r)
vector.x = r * vector.x / length;
vector.y = r * vector.y / length;

[delegate setPoint:CGPointMake(center.x + vector.x, center.y + vector.y)];

I think the best way to limit location to a circle is calculate vector from center to touch location. Calculate vector length then divide it by that length so it would be normalized. Then multiply normalized vector by radius of circle and finally add this vector to the center to compute new location.

CGPoint touch, center;
CGPoint vector = CGPointMake(touch.x-center.x, touch.y-center.y);

float length = sqrtf(vector.x*vector.x + vector.y*vector.y); 
// Normalize and multiply by radius (r)
vector.x = r * vector.x / length;
vector.y = r * vector.y / length;

[delegate setPoint:CGPointMake(center.x + vector.x, center.y + vector.y)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文