计算饼图中的旋转角度
我想围绕其中心点旋转图像。我面临的问题是我需要获取在触摸移动事件中计算的角度(我不想使用多点触摸)。我当前使用下面的代码
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
gestureStartPoint = gestureMovedPoint;//i am getting the gestureStartPoint on touch began event
gestureMovedPoint = [[allTouches objectAtIndex:0] locationInView:[self superview]];
NSLog(@"gestureMovedPoint = %@",NSStringFromCGPoint(gestureMovedPoint));
}
CGFloat previousAngle = [self angleBetweenPoints:gestureStartPoint second11:gestureMovedPoint]; // atan2(gestureMovedPoint.y - gestureStartPoint.y, gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat currentAngle =atan2(self.transform.b, self.transform.a);//atan2(gestureMovedPoint.y - gestureStartPoint.y,gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat angleToRotate = currentAngle - previousAngle;
float xpoint = (((atan2((gestureMovedPoint.x - gestureStartPoint.x) , (gestureMovedPoint.y - gestureStartPoint.y)))*180)/M_PI);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleToRotate-100);
self.transform = transform;
请帮助我找到解决方案,因为我被困在这里,需要在截止日期后尽快完成此申请。 提前致谢
I want to rotate the image around its center point.The problem i am facing is i need to get the angle to calculate in touch moved event (i dont want to use multi touch).I am current using the below code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
gestureStartPoint = gestureMovedPoint;//i am getting the gestureStartPoint on touch began event
gestureMovedPoint = [[allTouches objectAtIndex:0] locationInView:[self superview]];
NSLog(@"gestureMovedPoint = %@",NSStringFromCGPoint(gestureMovedPoint));
}
CGFloat previousAngle = [self angleBetweenPoints:gestureStartPoint second11:gestureMovedPoint]; // atan2(gestureMovedPoint.y - gestureStartPoint.y, gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat currentAngle =atan2(self.transform.b, self.transform.a);//atan2(gestureMovedPoint.y - gestureStartPoint.y,gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat angleToRotate = currentAngle - previousAngle;
float xpoint = (((atan2((gestureMovedPoint.x - gestureStartPoint.x) , (gestureMovedPoint.y - gestureStartPoint.y)))*180)/M_PI);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleToRotate-100);
self.transform = transform;
Kindly help me find the solution as i am stuck here and need to complete this application very soon as there is a dead line.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很高兴我记得三角函数
一旦你有了好的方法,只需在触摸事件方法中执行此操作即可。 (我忘了它叫什么...)
这几乎是您需要的所有代码。数学是正确的,但我不确定 setTransform 的东西。我在学校用浏览器写这个。从这里你应该能够弄清楚。
祝你好运,
奥鲁姆·阿奎拉
Glad I remember triginometry
Once you have your nice method, just do this in the touch event method. (I forgot what it's called...)
This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.
Good luck,
Aurum Aquila
必须思考这一点。但我更喜欢通过两次触摸来旋转视图。会简单很多。
Have to think at this. But I will prefer rotating the view with two touches. It will be much simpler.
我确实在如何获得触摸驱动的旋转方面遇到了一些困难,更重要的是因为我希望 100% 理解我正在使用的代码。因此,在多次失败的尝试之后,我得出了这样的结论:
如果有人对这种方法有更好的名称,我会洗耳恭听。
它的作用是获取父视图坐标中的一个点,相对于视图中心(位于父视图坐标中)重新映射它,并计算该重新映射点与轴 [0X] 之间的角度。为此,它将 y 标准化为正常的数学坐标(y 在其值增加时上升,而不是下降),因此
self.center.y - point.y
而不是相反。最后,在touchesMoved中:
I did struggle a bit with how to get a touch driven rotation, even more so because I want 100% understanding of the code I am using. So I ended up, after many failed attempts, with this:
If anyone has a better name for this method, I'm all ears.
What it does is that it takes a point in parent view coordinates, remaps it relative to the center of the view (which is in parent view coordinate), and computes the angle between this remapped point and the axis [0X]. To do so, it normalizes y to the normal mathematical coordinates (y goes up when its value increases, not down), hence
self.center.y - point.y
and not the opposite.Finally, in touchesMoved: