2013-03-07 104 views
0

我需要使用contentMode UIViewContentModeTopLeft在UIImageView中居中並縮放UIImage,只需使用CGAffineTransform。我無法使用contentMode UIViewContentModeCenter,因爲我需要完整的CGAffineTransform信息。如何在UIImageView中居中縮放(旋轉)UIImage

到目前爲止,我正在使用類似這樣的地方_imageView是一個UIImageView和圖像的UIImage。

_imageView.image = image; 
CGFloat scale = fmaxf(_imageView.bounds.size.width/_imageView.image.size.width, _imageView.bounds.size.height/_imageView.image.size.height); 
CGAffineTransform transform = CGAffineTransformIdentity; 
transform = CGAffineTransformTranslate(transform, _imageView.image.size.width * 0.5, _imageView.image.size.height * 0.5); 
transform = CGAffineTransformScale(transform, scale, scale);  
_imageView.transform = transform; 

縮放比較好,但圖像總是偏右下角。 有沒有人知道如何做到這一點,而不使用contentMode UIViewContentModeCenter?

回答

2

試試這個:

CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; 
scaleAnim.removedOnCompletion = YES; 
[_imageView.layer addAnimation:scaleAnim forKey:nil]; 
+0

您也可以嘗試與CALayer的其[anchorPoint]玩(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/ CoreAnimationBasics/CoreAnimationBasics.html#// apple_ref/DOC/UID/TP40004514-CH2-SW15)。 – 2013-03-07 21:36:41