UIRotationGestureRecognizer

时间:2012-07-23 06:53:38

标签: iphone objective-c uigesturerecognizer

我有一个非常奇怪的问题,我在我的应用程序中实现UIRotationGestureRecognizer来旋转图像。我的问题是,有时当你开始旋转图像时,它会跳到另一个手指。 示例:我用一根手指拖动我的图像,之后我用我的第二根手指开始旋转,但图像转到第二根手指。这是正常的吗?可以修复吗? 我正在使用的代码是:

if([recognizer state] == UIGestureRecognizerStateEnded) {

    prevRotation = 0.0;
    return;
}

CGFloat newRotation = 0.0 - (prevRotation - [recognizer rotation]);

CGAffineTransform currentTransformation = image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);

image.transform = newTransform;

prevRotation = [recognizer rotation];

我尝试保存中心的最后位置并再次应用它,但它也不起作用,如下所示:

lastPoint=image.center;

if([recognizer state] == UIGestureRecognizerStateEnded) {

    prevRotation = 0.0;
    return;
}

CGFloat newRotation = 0.0 - (prevRotation - [recognizer rotation]);

CGAffineTransform currentTransformation = image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);

image.transform = newTransform;

prevRotation = [recognizer rotation];
image.center=lastPoint;

1 个答案:

答案 0 :(得分:0)

试试这段代码:

-(void)rotate:(id)sender
{

    [self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

    self.imageTag = [[sender view]tag];
    NSLog(@"sender tag %d", self.imageTag);
    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        lastRotation = 0.0;
        return;
    }

    CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

    lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}