2014-05-12 75 views
0

嗨,
我想旋轉我的UIImageView而不移動整個「PNG」。沒有代碼只是測試會發生什麼 _fanImage.transform = CGAffineTransformMakeRotation(45);UIImageView旋轉而不移動圖像

它會變成整個圖像移動。我能做些什麼,這不會發生?

+0

我不明白你的問題。你的意思是圖像視圖應該旋轉,但在圖像視圖中設置的圖像不應該? – dasdom

+0

不僅圖像旋轉不是整個ImageView,因爲當ImageView旋轉圖像在其他地方移動 – user3195648

+0

我不認爲這是可能的。我不知道將圖像直接繪製到視圖上並旋轉視圖時會發生什麼。應該看起來與圖像視圖相同。但我仍然沒有明白你的意思是「圖像在其他地方移動」。 – dasdom

回答

0

你可以嘗試這樣的事情。你應該旋轉UIImage而不是UIImageView

- (UIImage *)imageWithTransform:(CGAffineTransform)transform { 


    CGRect rect = CGRectMake(0, 0, self.size.height, self.size.width); 
    CGImageRef imageRef = self.CGImage; 

    // Build a context that's the same dimensions as the new size 
    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
               self.size.width, 
               self.size.height, 
               CGImageGetBitsPerComponent(imageRef), 
               0, 
               CGImageGetColorSpace(imageRef), 
               CGImageGetBitmapInfo(imageRef)); 

    // Rotate and/or flip the image if required by its orientation 
    CGContextConcatCTM(bitmap, transform); 



    // Draw into the context; this scales the image 
    CGContextDrawImage(bitmap, rect, imageRef); 

    // Get the resized image from the context and a UIImage 
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    // Clean up 
    CGContextRelease(bitmap); 
    CGImageRelease(newImageRef); 

    return newImage; 
} 
+0

謝謝我發現了一個類似的教程現在可以工作,但現在UIImageView不再對觸摸做出反應了。 – user3195648

+0

好的,但這是你第一次迴應觸摸,並且你沒有顯示任何與處理觸摸相關的代碼。 –

0

我想你的意思是你想你的圖像以繞它的中心點。是對的嗎?如果是這樣,那麼默認情況下視圖應該這樣做。

您應該在Xcode中進行「翻譯,縮放和旋轉視圖」的搜索並閱讀所得到的文章。

請注意,所有iOS的角度均以弧度指定,而不是度數。

您的示例圖像並不真正有用,因爲我們無法看到圖像視圖被繪製到的框架。根據從Dropbox鏈接的圖片,幾乎不可能知道您的圖片視圖正在做什麼以及他們應該做什麼。

完整的360度是2pi。

您應該使用

CGFloat degrees = 45; 
CGFloat radians = degrees/180*M_PI; 
_fanImage.transform = CGAffineTransformMakeRotation(radians); 

這將解決您的代碼的旋轉量,但可能不會旋轉位置。

+0

謝謝,不,那也不行。 我找到了一個教程,現在它的工作原理與@iphonic的答案非常相似:) – user3195648

+0

含義是什麼? 「不起作用」是什麼意思?在你說出「不起作用」或「現在不響應觸摸」之類的事情之前,你需要更好地解釋你所遇到的問題。 –