gpt4 book ai didi

iphone - 计算饼图中的旋转角度

转载 作者:行者123 更新时间:2023-12-03 21:08:11 27 4
gpt4 key购买 nike

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

- (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;

请帮助我找到解决方案,因为我被困在这里,并且需要在截止日期后尽快完成此申请。提前致谢

最佳答案

很高兴我记得三角函数


-(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,<br/> [self degreesTorotateObjectWithPosition:view.frame.origin <br/> andTouchPoint:[touch locationInView:<b>parentView</b>]]

//<b>Note: parentView = The view that your object to rotate is sitting in.</b>

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

祝你好运

金天鹰

关于iphone - 计算饼图中的旋转角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5102942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com