我将UIView子类化为avatar-object,其中包含一个图标(UIImageView),名称(UILabel)和ID(UILabel)。根据方向X,Y坐标,我想要旋转图标。例如,假设orientation.X = 1并且orientation.Y = 1将给出45度角。 X和Y可以是介于-1和1之间的任何值。
我不确定它是如何以数学方式计算的,也不是cocoa API要求它。任何想法从哪里开始?
答案 0 :(得分:0)
你需要研究三角函数 - 特别是acos
和asin
。
This article可能会有所帮助。
答案 1 :(得分:0)
为了平滑,我添加了一个动画:
- (void)rotateView:(UIView *)view withDuration:(NSTimeInterval)duration andRadians:(CGFloat)radians
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeRotation(radians);
view.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
编辑:
对于您需要创建自己的旋转矩阵的其他角度。
阅读here如何在Objective-c
中创建自定义仿射变换希望这有帮助。