以下是在触摸事件上旋转箭头(uiimage)的代码。在这我减去两个触摸的位置之前和当前但问题是箭头(uiimage)有时会在触摸的相反方向上移动。
- (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);
}
}