【问题标题】:CGAffineTranformRotate atan2 inaccurationCGAffineTranformRotate atan2 不准确
【发布时间】:2012-02-04 16:07:41
【问题描述】:

我正在转换我的视图并且总是发生一些奇怪的事情,我的意思是我使用以下代码计算角度:angle = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

视图旋转但不正确。我的意思是它以正确的方向旋转,但角度总是不准确,大约 +/- 0.05 弧度。当我再次点击时,视图会旋转到适当的位置。有什么建议吗?将角度精确到逗号后 5 个位置对我来说很重要。

Some NSLog to show you the problem:

First rotation first tap and second tap
2012-01-07 01:01:26.283 Wheel[9080:707] Angle: 0.598412 
2012-01-07 01:01:29.281 Wheel[9080:707] Angle: -0.070008
Second rotation first tap and second tap
2012-01-07 01:01:31.103 Wheel[9080:707] Angle: -0.679809 
2012-01-07 01:01:32.450 Wheel[9080:707] Angle: 0.092595
Third rotation first tap and second tap
2012-01-07 01:01:35.745 Wheel[9080:707] Angle: 0.607844 
2012-01-07 01:01:36.945 Wheel[9080:707] Angle: -0.064927 
Fourth rotation first tap and second tap
2012-01-07 01:01:41.073 Wheel[9080:707] Angle: -0.635756 
2012-01-07 01:01:41.920 Wheel[9080:707] Angle: 0.052361 

我忘了告诉你,条件,点之间的差异越远,误差越大。

编辑:

Circle *view = (Circle *) [self view];



for (CircleThumb *thumb in view.subviews) {


    CGPoint point = [thumb convertPoint:thumb.centerPoint toView:nil];
    CircleThumb *shadow = [[view.overlayView subviews] lastObject];
    CGPoint centralPoint = [shadow convertPoint:shadow.centerPoint toView:nil];
    CGRect shadowRect = [shadow.superview convertRect:shadow.frame toView:nil];

    if (CGRectContainsPoint(shadowRect, point) == YES) {
        CGPoint pointInShadowRect = [thumb convertPoint:thumb.centerPoint toView:shadow];
        if (CGPathContainsPoint(shadow.arc.CGPath, NULL, pointInShadowRect, NULL)) {


            CGAffineTransform current = view.transform;
            CGPoint center = view.window.center;
            CGPoint currentTouchPoint =centralPoint;
            CGPoint previousTouchPoint = point;

          long double angle = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

            [UIView animateWithDuration:0.3f animations:^{
                [view setTransform:CGAffineTransformRotate(current, angle)];
            }];
            [view.delegate circle:view didMoveToSegment:thumb.tag thumb:thumb];


            NSLog(@"Angle: %Lf ", angle);
            break;
        }
    }            
}

这是'- touchesEnded: withEvent:' 实现的一部分的代码

我正在制作类似于 Convert bot 中的控制。我的应用程序和 convertbot 的“轮子”看起来很相似,但我的使用自定义绘图。

所以 Circle 是一个我们旋转的 UIView。 Circle 有子视图 - CircleThumbs。拇指代表圆的单个部分。分数是正确计算的,但我不会解释为什么,因为没有必要。

【问题讨论】:

  • 您能否发布打印上述信息的代码?什么是第一次和第二次点击?

标签: iphone ios uiview core-graphics cgaffinetransform


【解决方案1】:

atan 计算永远不会完全正确。然后你让 iOS 再次计算出角度的 cos 和 sin(cgaffinetransformRotate 就是这样做的)。所以你在堆积三角函数的不准确性。而且由于您仅计算与前一个角度的差异,我想您还会在多个触摸事件上累积不准确性。

如果要旋转某物,则没有理由使用三角函数或角度。您可以使用线性代数完全实现这一点。像这样的:

v.x = touch.x - center.x; v.y = touch.y - center.y;
float length = sqrt(v.x*v.x + v.y* v.y);
if (length < 5 ) break;
v.x /= length; v.y /= length;

// the rotation matrix is
//  v.x -v.y
//  v.y  v.x

rot.x = previous.x * v.x + previous.y * v.y;
rot.y = previous.x * v.y - previous.y * v.x;

CGAffineTransform rotate = CGAffineTransformMake( rot.x, rot.y, -rot.y, rot.x, 0,0);
...
[view SetTransform:CGAffineTransformConcat ( current, rotate )];
...

previous.x = v.x;
previous.y = v.y;

此伪代码计算当前点的归一化向量(也存储了前一点的向量)。然后我们定义一个矩阵,将前一个向量旋转成新向量。该矩阵与现有转换连接。

如果不总是从前一个状态计算旋转,而是从初始状态计算旋转会更好。诀窍是只为达阵计算“先前”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2011-05-04
    • 2023-03-16
    • 1970-01-01
    • 2013-09-21
    • 2021-07-28
    • 2013-09-22
    相关资源
    最近更新 更多