wcz*_*ski 4 iphone core-graphics uiview cgaffinetransform ios
我正在转换我的视图并且总是发生一些奇怪的事情,我的意思是我使用这个代码来计算角度: 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
Run Code Online (Sandbox Code Playgroud)
我忘了告诉你,条件,点之间的差异越大,误差就越大.
编辑:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码,它是' - touchesEnded:withEvent:'实现的一部分
我正在控制类似于Convert bot中的控件.我的应用程序和convertbot的"轮子"看起来很相似,但我的使用自定义绘图.
所以Circle是我们旋转的UIView.Circle有子视图 - CircleThumbs.拇指代表圆圈的单个部分.积分计算得当但我不会解释原因,因为没有必要.
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;
Run Code Online (Sandbox Code Playgroud)
该伪代码计算当前点的归一化向量(也存储了前一个点的归一化向量).然后我们定义一个矩阵,将前一个向量旋转到新的向量中.该矩阵与现有转换连接在一起.
如果旋转不总是从先前状态计算,而是从初始状态计算,则会更好.诀窍是仅为触地得分计算"先前".
| 归档时间: |
|
| 查看次数: |
852 次 |
| 最近记录: |