默认旋转和缩放后,平移手势表现得很奇怪

时间:2013-06-18 21:33:04

标签: ios rotation scale pan

我目前正在设计一个小游戏作为学习项目,它基本上如下,一个图像在viewDidLoad上旋转和缩放,另一个图像是原始图像的直接副本。

所以基本上有一个图像与另一个图像略有不同,目标是将其缩小,旋转并在另一个图像的顶部移动5像素,5度旋转和5%规模。

我遇到了一个问题。我使用以下代码“倾斜”图像......

CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2.5);
image.transform = CGAffineTransformScale(transform, 1.25, 1.25);

旋转图像然后将其缩放125%后,我的平移手势无法正常执行。

有谁知道这里会发生什么?错误地说,我的意思是它不会用我的手指移动..它似乎滑行或朝相反方向移动。 Video

我的平移手势方法如下。

if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gesture translationInView:image];
        //if within game field
        if((image.center.x + translation.x) > 50.0 && (image.center.x + translation.x) < 255.0 && (image.center.y + translation.y) > 50.0 && (image.center.y + translation.y) < 302) {
            [image setCenter:CGPointMake([image center].x + translation.x, [image center].y + translation.y)]; //move it
        }
    }
    [gesture setTranslation:CGPointZero inView:[image superview]];
    if(gesture.state == UIGestureRecognizerStateEnded) [self didWin]; // not relevant to question

在旋转和缩放图像后,有谁知道为什么平底锅操作不正确?当我注释掉前两行代码时,平移器会正确执行,并随着用户的手指移动。

提前感谢任何建议或帮助!

2 个答案:

答案 0 :(得分:1)

旋转可能已经改变了框架。我使用 cosf sinf 函数来处理它。

handlePan和handleRotate是回调函数,我可以在其中控制self.view的子视图。在这里,您应该用自己的视图替换图像

static CGFloat _rotation = 0;

- (void)handlePan:(UIPanGestureRecognizer*)recognizer
{
    UIImageView *image = nil;
    for (UIImageView *tmp in recognizer.view.subviews) { // pick the subview
        if (tmp.tag == AXIS_TAG) {
            image = tmp;
        }
    }
    CGPoint translation = [recognizer translationInView:image];

    // I have found any related documents yet, but these equations do work!//////
    CGFloat dx = translation.x * cosf(_rotation) - translation.y*sinf(_rotation);
    CGFloat dy = translation.x * sinf(_rotation) + translation.y*cosf(_rotation);
    /////////////////////////////////////////////////////////////////////////////

    image.center = CGPointMake(image.center.x+dx, dy+image.center.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:image];
}

- (void)handleRotate:(UIRotationGestureRecognizer*)recognizer
{
    UIImageView *image = nil;
    for (UIImageView *tmp in recognizer.view.subviews) { // pick the subview
        if (tmp.tag == AXIS_TAG) {
            image = tmp;
        }
    }
    CGFloat r = [recognizer rotation];
    if ((recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) 
        && recognizer.numberOfTouches == 2) {
        image.transform = CGAffineTransformMakeRotation(_rotation+r);
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        _rotation+=r; // Record the final rotation
    }
}

答案 1 :(得分:0)

解决方案是稍微改变泛码...

if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gesture translationInView:self.view]; //CHANGED
        //if within game field
        if((image.center.x + translation.x) > 50.0 && (image.center.x + translation.x) < 255.0 && (image.center.y + translation.y) > 50.0 && (image.center.y + translation.y) < 302) {
            [image setCenter:CGPointMake([image center].x + translation.x, [image center].y + translation.y)]; //move it
        }
    }
    [gesture setTranslation:CGPointZero inView:self.view];

已更改 in view:self.viewtranslationInView:self.view];