0

我实现了一个 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;
}
4

1 回答 1

0

我做了一些记录,似乎我的 uiview 的中心在触摸拖动事件期间发生了变化。因此,我使用 touches started 方法存储了 uiview 的中心并使用它来代替。

- (void)rotateItem:(UIPanGestureRecognizer *)recognizer
    {
        NSLog(@"Rotate Item");

        CGPoint superPoint = [self convertPoint:itemCenter toView:self.superview];

        float ang = atan2([recognizer locationInView:self.superview].y - superPoint.y, [recognizer locationInView:self.superview].x - superPoint.x);

        float angleDiff = deltaAngle - ang;
        self.transform = CGAffineTransformRotate(startTransform, -angleDiff);

    }

    #pragma mark - Touch Methods

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)recognizer
    {

        if (recognizer == rotateGesture) {
            NSLog(@"rotate gesture started");

            deltaAngle = atan2([recognizer locationInView:self.superview].y-self.center.y, [recognizer locationInView:self.superview].x-self.center.x);

            startTransform = self.transform;
        }

        return YES;
    }
于 2012-07-06T20:24:38.317 回答