這是一個老問題,但其他的解決方案並沒有爲我工作得很好,所以我想出了另一種解決方案:
旋轉圖像本質上是應用只是一個翻譯正常旋轉,確保你想旋轉的點在旋轉後仍然在同一個點上。爲此,請在旋轉前計算圖像中位置的CGPoint,在旋轉後獲取位置,並將差異作爲圖像上的平移,將其「捕捉」到正確的位置。下面是我一直在使用的代碼:
請記住,翻譯應通過CGAffineTransform應用,不要移動.center,因爲翻譯需要相對於旋轉,而CGAffineTransformTranslate()需要關心這一點。
// Note: self is the superview of _imageView
// Get the rotation point
CGPoint rotationPointInSelf = self.center; // or whatever point you want to rotate around
CGPoint rotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];
// Rotate the image
_imageView.transform = CGAffineTransformRotate(_imageView.transform, angle);
// Get the new location of the rotation point
CGPoint newRotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];
// Calculate the difference between the point's old position and its new one
CGPoint translation = CGPointMake(rotationPointInImage.x - newRotationPointInImage.x, rotationPointInImage.y - newRotationPointInImage.y);
// Move the image so the point is back in it's old location
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, -translation.x, -translation.y);
謝謝我正要發佈子視圖解決方案,我發現了:) – Jan