使用CGAffineTransformTranslate旋转后,将图像移动到其原始尺寸空间

时间:2015-08-31 10:38:29

标签: ios ios8 rotation translation cgaffinetransform

我正在使用以下库来旋转,缩放和移动图像ZoomRotatePanImageView

问题在于,如果在旋转后使用动态图像,它会尝试根据图像的新角度而不是平移手势方向移动图像,则会错过运动图像。

即。当将图像旋转180度然后尝试将其向右移动时,它向左移动(所有方向都反转)

以下是使用UIPanGestureRecognizer操纵图片的CGAffineTransformTranslate代码

- (IBAction) handlePan:(UIPanGestureRecognizer*)recogniser {
    if (recogniser.state == UIGestureRecognizerStateCancelled) {
        recogniser.view.transform = originalImageViewTransform;
        return;
    }

    if (recogniser.state == UIGestureRecognizerStateBegan) {
        originalImageViewTransform = recogniser.view.transform;
    }

    CGAffineTransform transform = recogniser.view.transform;
    CGPoint translation = [recogniser translationInView:self.superview];
    transform = CGAffineTransformTranslate(transform, translation.x, translation.y);


    [recogniser setTranslation:CGPointZero inView:self]; // This line means we don't need prevRotation

    if (recogniser.state == UIGestureRecognizerStateEnded) {
        // The gesture ended, so push an undo action before setting the transform.
        [self setTransform:transform ofView:recogniser.view undoTransform:originalImageViewTransform];
        if ([self.delegate respondsToSelector:@selector(saveImageStateWithTag:)])
            [self.delegate saveImageStateWithTag:self.tag];
    } else {
        // The gesture changed but didn't end, so don't push an undo action.
        recogniser.view.transform = transform;
    }
}

那么如何使图像根据原始尺寸空间而不是新尺寸空间移动?

1 个答案:

答案 0 :(得分:1)

更改以下行
CGPoint translation = [recogniser translationInView:self.superview];

CGPoint translation = [recogniser translationInView:self];

将解决您的问题