我正在转换我的视图,总是发生一些奇怪的事情,我的意思是,我用以下代码计算角度: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:‘实现的一部分
我做的控制类似于在转换机器人。从我的应用程序和从转换机器人的“车轮”看起来类似,但我的使用自定义绘图。
所以圆是一个UIView,我们旋转。圆圈有子视图- CircleThumbs。拇指代表圆圈的单个部分。点数是计算正确的,但我不会解释为什么,因为没有必要。
发布于 2012-01-07 08:45:40
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;此伪代码计算当前点的归一化向量(前一点的向量也被存储)。然后,我们定义了一个矩阵,将前一个向量旋转成新的向量。该矩阵与现有的转换连接在一起。
如果旋转不是总是根据以前的状态来计算,而是从初始状态计算,那就更好了。诀窍是只为触地得分计算“先前”。
https://stackoverflow.com/questions/8766128
复制相似问题