使用 CGAffineTransformTranslate 旋转后将图像移动到其原始维度 space

Move image accodring to its original dimenstional space after rotating using CGAffineTransformTranslate

我正在使用以下库来旋转、缩放和移动图像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;
    }
}

那么如何让图像按照原来的维度space而不是新的维度移动呢?

更改以下行

CGPoint translation = [recogniser translationInView:self.superview];

CGPoint translation = [recogniser translationInView:self];

将解决您的问题