我实现了一个 UIPanGestureRecognizer,因为我希望用一根手指沿其轴旋转 UIView。uiview 中的按钮开始 UIView 旋转的手势。问题是它只有在按钮位于左上角的第一象限时才能正确旋转。任何其他象限,它都会不规律地旋转。谁能告诉我我的数学有什么问题。顺便说一下,ang 使用超级视图的坐标计算角度,因为用户的手指可能在旋转视图范围之外,但这可能不是必需的。
谢谢你
- (void)rotateItem:(UIPanGestureRecognizer *)recognizer
{
NSLog(@"Rotate Item");
float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x);
float angleDiff = deltaAngle - ang;
self.transform = CGAffineTransformRotate(startTransform, -angleDiff);
CGFloat radians = atan2f(self.transform.b, self.transform.a);
NSLog(@"rad is %f", radians);
}
#pragma mark - Touch Methods
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)recognizer
{
if (recognizer == rotateGesture) {
NSLog(@"rotate gesture started");
deltaAngle = atan2([recognizer locationInView:self].y-self.center.y, [recognizer locationInView:self].x-self.center.x);
startTransform = self.transform;
}
return YES;
}