计算饼图中的旋转角度

发布于 2024-10-19 03:53:50 字数 1226 浏览 2 评论 0原文

我想围绕其中心点旋转图像。我面临的问题是我需要获取在触摸移动事件中计算的角度(我不想使用多点触摸)。我当前使用下面的代码

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞取 2024-10-26 03:53:50

很高兴我记得三角函数


-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{

   float dX = touchPoint.x-objPos.x;        // distance along X
   float dY = touchPoint.y-objPos.y;        // distance along Y
   float radians = atan2(dY, dX);          // tan = opp / adj

   //Now we have to convert radians to degrees:
   float degrees = radians*M_PI/360;

   return degrees;
}

一旦你有了好的方法,只需在触摸事件方法中执行此操作即可。 (我忘了它叫什么...)

CGAffineTransform current = view.transform;

[view setTransform:CGAffineTransformRotate(current,
[self degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]] //Note: parentView = The view that your object to rotate is sitting in.

这几乎是您需要的所有代码。数学是正确的,但我不确定 setTransform 的东西。我在学校用浏览器写这个。从这里你应该能够弄清楚。

祝你好运,

奥鲁姆·阿奎拉

Glad I remember triginometry


-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{

   float dX = touchPoint.x-objPos.x;        // distance along X
   float dY = touchPoint.y-objPos.y;        // distance along Y
   float radians = atan2(dY, dX);          // tan = opp / adj

   //Now we have to convert radians to degrees:
   float degrees = radians*M_PI/360;

   return degrees;
}

Once you have your nice method, just do this in the touch event method. (I forgot what it's called...)

CGAffineTransform current = view.transform;

[view setTransform:CGAffineTransformRotate(current,
[self degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]] //Note: parentView = The view that your object to rotate is sitting in.

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

梦醒时光 2024-10-26 03:53:50

必须思考这一点。但我更喜欢通过两次触摸来旋转视图。会简单很多。

Have to think at this. But I will prefer rotating the view with two touches. It will be much simpler.

恋你朝朝暮暮 2024-10-26 03:53:50

我确实在如何获得触摸驱动的旋转方面遇到了一些困难,更重要的是因为我希望 100% 理解我正在使用的代码。因此,在多次失败的尝试之后,我得出了这样的结论:

- (CGFloat) pointToAngleFromCenter: (CGPoint) point {
    // transform point to a self.center'ed origin based coordinate system
    point.x = point.x - self.center.x ;
    // ditto for y, but compensate for y going downwards to y going upwards
    point.y = self.center.y - point.y ;
    return ::atan2(point.y, point.x) ;
}

如果有人对这种方法有更好的名称,我会洗耳恭听。

它的作用是获取父视图坐标中的一个点,相对于视图中心(位于父视图坐标中)重新映射它,并计算该重新映射点与轴 [0X] 之间的角度。为此,它将 y 标准化为正常的数学坐标(y 在其值增加时上升,而不是下降),因此 self.center.y - point.y 而不是相反。

最后,在touchesMoved中:

- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event {
    UITouch * touch = [touches anyObject] ;

    CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:self.superview]] ;
    CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:self.superview]] ;
    // the current value of the rotation angle
    CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ;
    // the angle difference between last touch and the current touch
    CGFloat diffA = currA - prevA ;
    // the new angle resulting from applying the difference
    CGFloat angle = tranA - diffA ;

    CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ;

    self.transform = t ;
    [self setNeedsDisplay] ;
}

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:

- (CGFloat) pointToAngleFromCenter: (CGPoint) point {
    // transform point to a self.center'ed origin based coordinate system
    point.x = point.x - self.center.x ;
    // ditto for y, but compensate for y going downwards to y going upwards
    point.y = self.center.y - point.y ;
    return ::atan2(point.y, point.x) ;
}

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:

- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event {
    UITouch * touch = [touches anyObject] ;

    CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:self.superview]] ;
    CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:self.superview]] ;
    // the current value of the rotation angle
    CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ;
    // the angle difference between last touch and the current touch
    CGFloat diffA = currA - prevA ;
    // the new angle resulting from applying the difference
    CGFloat angle = tranA - diffA ;

    CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ;

    self.transform = t ;
    [self setNeedsDisplay] ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文