问题描述
对于某些人来说,这肯定是微不足道的数学知识.
For sure this is very trivial math stuff for some of you.
但是:当我旋转视图时,可以说从0度开始并向前旋转0.1、1、10、100、150、160、170、179、179,999,然后沿相同方向继续旋转,这种情况会发生,说清楚一点:砰! BAAAAAAAAANNNNNGGGG !!!! -179,9999,-170,-150,-50、0、50、100、170、179,99以及再次:BBBBAAAANNNGGG !!! -179,999,-170,-100等,以确保您知道我的意思;-)
But: When I rotate a view, lets say starting at 0 degrees and rotating forward 0.1, 1, 10, 100, 150, 160, 170, 179, 179,999, and keeping on rotating in the same direction, this happens, to say it loud and clear: BANG !!!! BAAAAAAAAANNNNNGGGG!!!! -179,9999, -170, -150, -50, 0, 50, 100, 170, 179,99, and again: BBBBAAAANNNGGG!!! -179,999, -170, -100, and so on... for sure you know what I mean ;-)
想象一下,您在道路上行驶时,里程表突然跳为负数.你吓坏了吧?
Imagine you driving on a road and suddenly your miles-o-meter jumps negative like that. You wold freak out, right?
你知道吗?这对我的算法非常不利.我不知道该如何解决这个问题,除了检查我的值是否突然交换了if-else-blocks之外.有什么告诉我,我必须看一些数学函数,例如窦波和其他东西.但是我的数学知识真是太烂了.
And you know what? This is VERY bad for my algorithm. I have no idea how to resolve this other than checking in if-else-blocks if my value suddenly swapped over. Something tells me I have to look at some math functions like sinus waves and other stuff. But my math knowledge sucks to the highest degree.
我该如何解决?我尝试计算角度之间的距离,即我有两个视图,一个位于另一个视图上,两个视图都旋转了.我想计算两个视图的整体旋转.值突然从-179,99999变为179,99999时,这个小小的但疯狂的事情破坏了我的计算.另外,如果您知道我的意思,我也不知道180是否存在,或者是否在fabsf(179,9999999999999999999999999)处交换了东西.
How can I solve this out? I try to calculate distances between angles, i.e. I have two views where one resides on the other, and both are rotated. And I want to calculate the overall rotation of both views. This little but mad thingy destroys my calculations as soon as values swap over suddenly from -179,99999 to 179,99999. Also I don't know if a 180 exists or if things swap somewhere at fabsf(179,999999999999999999999) if you know what I mean.
例如:-170度减去50度是多少?好吧,我认为是-220度.但是当我旋转-170-50时,最终得到的是这个值:40度.
For example: What would be -170 degrees minus 50 degrees? Well, I think -220 degrees. But instead when I rotate -170 - 50 I end up getting this value: 40 degrees.
在没有任何不安全的非逻辑交换逻辑的情况下,如何获得-220而不是40的期望值?有时我的自制交换器工作正常,但有时由于数学上的原因而无效.
How can I receive what I expect, the -220 instead of 40, without any kind of unsecure swapping-if-else logic? Sometimes my selfmade-swapper works, but sometimes it doesnt due to math incprecisions.
编辑:我根据视图图层的transform.rotation.z属性计算角度.
I calculate the angles from the transform.rotation.z property of the view's layers.
推荐答案
视图不是数据的规范表示,查询仅告诉您系统的可视状态.如果您关心角度的特定表示形式,那么就不应该从transform读取它,而应该将其适当地存储在模型或控制器中,并根据该角度设置视图的状态.
The view is not a canonical representation of your data, querying tells you about visual state of the system, nothing more. If you care about a particular representation of the angle then you shouldn't be reading it from transform, you should be storing it in your model or controller as appropriate, and setting your view's state based on that.
@interface MyViewController: UIViewController {
CGFloat rotation;
}
@property (nonatomic) CGFloat rotation;
@end
@implementation MyViewController
@synthesize rotation;
- (void)setRotation:(CGFloat)rotation_ {
rotation = rotation_;
// Do whatever math you want and set the transform of the view based on that;
}
@end
这篇关于如何解决这个轮换问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!