Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UILongPressGestureRecognitor更精确的CGPoint_Objective C_Cocoa Touch_Uigesturerecognizer_Cgpoint - Fatal编程技术网

Objective c UILongPressGestureRecognitor更精确的CGPoint

Objective c UILongPressGestureRecognitor更精确的CGPoint,objective-c,cocoa-touch,uigesturerecognizer,cgpoint,Objective C,Cocoa Touch,Uigesturerecognizer,Cgpoint,我使用的是UILongPressGestureRecognitor,它工作得很好,但我得到的数据不够精确,不适合我的用例。我想我得到的分数是四舍五入的 我得到的示例点:100.5、103.0等。小数部分为.5或.0。有没有办法获得更精确的点?我希望像'100.8745'中那样的.xxxx,但是.xx可以 我之所以需要它,是因为我有一个圆形的UIBezierPath,我想将拖动动作限制为仅该圆形路径。该项只能沿该圆的圆周拖动。为此,我使用圆的半径计算了圆边界上的720个点。现在这些点是.xxxx数

我使用的是UILongPressGestureRecognitor,它工作得很好,但我得到的数据不够精确,不适合我的用例。我想我得到的分数是四舍五入的

我得到的示例点:100.5、103.0等。小数部分为.5或.0。有没有办法获得更精确的点?我希望像'100.8745'中那样的.xxxx,但是.xx可以

我之所以需要它,是因为我有一个圆形的UIBezierPath,我想将拖动动作限制为仅该圆形路径。该项只能沿该圆的圆周拖动。为此,我使用圆的半径计算了圆边界上的720个点。现在这些点是.xxxx数字。如果我把它们绕过来,阻力在圆圈中间部分就不那么平滑了。这是因为在中间部分赤道上X坐标上的点非常接近。因此,当我将y坐标四舍五入时,我丢失了很多点,因此出现了“不太平滑”的拖动动作

下面是我计算积分的方法

for (CGFloat i = -154;i<154;i++) {

    CGPoint point = [self pointAroundCircumferenceFromCenter:center forX:i];
    [bezierPoints addObject:[NSValue valueWithCGPoint:point]];
    i = i - .5;
}

- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center forX:(CGFloat)x
{
CGFloat radius = 154;
CGPoint upperPoint = CGPointZero;
CGPoint lowerPoint = CGPointZero;

//theta used to be the x variable. was first calculating points using the angle
/* point.x = center.x + radius * cosf(theta);
point.y = center.y + radius * sinf(theta);*/

CGFloat y = (radius*radius) - (theta*theta);
upperPoint.x = x+156;
upperPoint.y = 230-sqrtf(y);
lowerPoint.x = x+156;
lowerPoint.y = sqrtf(y)+230;

NSLog(@"x = %f, y = %f",upperPoint.x, upperPoint.y);
[lowerPoints addObject:[NSValue valueWithCGPoint:lowerPoint]];
[upperPoints addObject:[NSValue valueWithCGPoint:upperPoint]];
return upperPoint;
}
基本上,我获取“接触点”的x坐标,并通过将其与我之前计算的点阵列进行比较来返回y

目前这段代码只适用于半个圆,因为每个x有2个y值,因为它是一个圆,忽略它,因为我认为这很容易处理


在图中,白色圆圈是原始圆圈,黑色圆圈是我从代码中得到的点的圆圈+对其进行格式化以消除精度以适应我得到的输入。如果你环视赤道(红色突出显示的部分),你会看到下几点之间有一个间隙。这个差距是我的问题。

回答您最初的问题:在带有视网膜显示器的设备上,一个像素是0.5点,因此
0.5
是您在这个硬件上可以获得的最佳分辨率。 (在非视网膜设备上,1像素=1点。)

但在我看来,你根本不需要点数组。如果正确理解问题,可以使用以下代码 将任意点“限制”(或“投影”)到圆的周长:

CGPoint center = ...; // Center of the circle
CGFloat radius = ...; // Radius of the circle
CGPoint point = ...;  // The touched point
CGPoint resultingPoint; // Resulting point on the circumference

// Distance from center to point:
CGFloat dist = hypot(point.x - center.x, point.y - center.y);
if (dist == 0) {
    // The touched point is the circle center.
    // Choose any point on the circumference:
    resultingPoint = CGPointMake(center.x + radius, center.y);
} else {
    // Project point to circle circumference:
    resultingPoint = CGPointMake(center.x + (point.x - center.x)*radius/dist,
                     center.y + (point.y - center.y)*radius/dist);
}

你为什么需要这样的精度?屏幕上的一个手指能如此精确吗?据我所知,在视网膜显示器上,一个像素代表0.5个点。“所以你永远也得不到更高的分辨率。”Daviddrönnqvist补充道。我想.xxxx太多了,但是.x声音reasonable@MartinR那太糟糕了。那我试试别的。如果你有一些可以工作的东西,欢迎你的建议。也许你应该发布一些代码。如何计算点,阻力如何限制在圆周上,等等…添加了一张图片来更好地解释这个问题。你的方式不会让它变慢吗?@nupac:为什么会变慢?这是一个简单的计算,而不是将一个点与720个其他点的数组进行比较。而且它避免了所有的舍入/精度问题,或者由于0.5点的“有限”分辨率而产生的问题。@nupac:我已经用新代码更新了答案,该代码避免了三角函数,并且可以执行得稍微好一点。有效且不慢。非常感谢。
CGPoint center = ...; // Center of the circle
CGFloat radius = ...; // Radius of the circle
CGPoint point = ...;  // The touched point
CGPoint resultingPoint; // Resulting point on the circumference

// Distance from center to point:
CGFloat dist = hypot(point.x - center.x, point.y - center.y);
if (dist == 0) {
    // The touched point is the circle center.
    // Choose any point on the circumference:
    resultingPoint = CGPointMake(center.x + radius, center.y);
} else {
    // Project point to circle circumference:
    resultingPoint = CGPointMake(center.x + (point.x - center.x)*radius/dist,
                     center.y + (point.y - center.y)*radius/dist);
}