我正在使用以下代码通过在左右箭头上开始手势来旋转图像。同时沿着旋转移动箭头。
- (void)handleRotation:(UIPanGestureRecognizer *)recognizer
{
UIView* cview = self.superview.superview;
UIView* lView = self.superview;
CGPoint origin = [lView convertPoint: self.center toView: cview];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
if (recognizer.view == leftImage)
{
initialP= [self convertPoint: leftImage.center toView: cview];
}
else
{
initialP = [self convertPoint: rightImage.center toView: cview];
}
}
CGPoint currentP = [recognizer translationInView:cview];
currentP.x += initialP.x;
currentP.y += initialP.y;
CGFloat angle = 0;
CGFloat a = initialP.x - origin.x;
CGFloat b = initialP.y - origin.y;
CGFloat c = currentP.x - origin.x;
CGFloat d = currentP.y - origin.y;
angle = acosf( ((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
angle += lastReleasedAngle;
if (currentP.y < (currentP.x *(initialP.y - origin.y) + (initialP.x * origin.y - initialP.y * origin.x)) / (initialP.x - origin.x))
{
angle = -angle;
}
if (recognizer.view == leftImage)
{
angle = -angle;
}
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
[self setTransform: CGAffineTransformMakeRotation(angle)];
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
lastReleasedAngle = angle;
}
第一次开始旋转时它工作得很好。说,右箭头现在是 0 度..当我从这里开始旋转并在这里释放它工作正常。当我在其他任何地方释放旋转然后尝试旋转时,图像开始精确翻转移动 180 度后到 180 度。