以下是在觸摸事件中旋轉箭頭(uiimage)的代碼。在這個我減去兩個觸摸前一個和當前的位置,但問題是箭頭(uiimage)有時在觸摸的相反方向移動。如何在iphone中速度計中旋轉圖像
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
NSLog(@"%@",[touch view]);
if ([touch view] == ImgViewArrowImage)
{
CGPoint currentLocation = [touch locationInView:touch.view];
CGPoint pastLocation = [touch previousLocationInView:touch.view];
CGPoint d1 = CGPointMake(currentLocation.x-touch.view.center.x, currentLocation.y-touch.view.center.y);
CGPoint d2 = CGPointMake(pastLocation.x-touch.view.center.x, pastLocation.y-touch.view.center.y);
CGFloat angle1 = atan2(d1.y, d1.x);
CGFloat angle2 = atan2(d2.y, d2.x);
[[ImgViewArrowImage layer] setAnchorPoint:CGPointMake(0.0, 0.5)];
[[ImgViewArrowImage layer] setPosition:CGPointMake(159,211)];
ImgViewArrowImage.transform = CGAffineTransformRotate(ImgViewArrowImage.transform, angle1-angle2);
}
}
當角度1,angle2進入零下可能發生。 – 2012-07-18 12:11:37
你Teodor,這就是我知道,減法去負...但我需要的解決方案.... – 2012-07-18 12:15:09
詹尼斯感謝編輯.. – 2012-07-18 12:16:46