我正在转换我的观点并且总是发生一些奇怪的事情,我的意思是我使用以下代码计算角度: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。拇指代表圆圈的单个部分。积分计算得当,但我不解释原因,因为没有必要。
答案 0 :(得分:7)
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;
此伪代码计算当前点的归一化向量(也存储前一个点的归一化向量)。然后我们定义一个矩阵,将前一个向量旋转到新的向量中。该矩阵与现有转换连接在一起。
如果旋转不总是从先前的状态计算,而是从初始状态计算,那会更好。诀窍是仅为触地得分计算“先前”。