![](/img/trans.png)
[英]Rotating a UIImage to point in the right direction defined by calculated angle
[英]Rotating a UIImage fixing angle turn
我有一個UIImage,我希望用戶能夠旋轉它,就像它是保險箱的旋轉按鈕一樣。 我不希望圖像在0 <x <360范圍內以任何角度旋轉,但是我希望滾輪(UIImage)停在固定點之一,固定點會有所不同(通常為0到20)。 例如,如果我有3個選項,則用戶將能夠轉動滾輪並將指針恰好放在三個點上(在這種情況下,角度為60、120、180度)。 我使用以下代碼旋轉圖像:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
int len = [allTouches count]-1;
UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
CGPoint location = [touch locationInView:[self.SplashItGroupPicker superview]];
float theAngle = atan2( location.y-self.SplashItGroupPicker.center.y, location.x-self.SplashItGroupPicker.center.x );
int totalRadians = -3.14;
totalRadians += fabs(theAngle - totalRadians);
totalRadians = fmod(totalRadians, 2*M_PI);
[self rotateImage:totalRadians];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) rotateImage:(float)angleRadians{
self.SplashItGroupPicker.transform = CGAffineTransformMakeRotation(angleRadians);
CATransform3D rotatedTransform = self.SplashItGroupPicker.layer.transform;
self.SplashItGroupPicker.layer.transform = rotatedTransform;
}
如何獲得該結果?
找出每個步驟需要多少弧度,例如,如果您想要4個位置,那么每個位置將比上一個位置多3.14 / 4弧度。 稱為radIncrement。
在這行之后
totalRadians = fmod(totalRadians, 2*M_PI);
將totalRadians除以radIncrement,將結果四舍五入為整數值,然后再次將其乘以radIncrement。 您可以將舍入行為更改為在更改位置時進行調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.