这个UIImageView有一个背景图片,我在InterfaceBuilder中设置。当我改变方向到横向时,图片被裁剪了。
我希望当iPad处于纵向模式时,UIImageView设置image-portrait.png,而当处于横向模式时,设置image-landscape.png。
感谢您的帮助和建议!
截图:
我找到了一个解决方案:
在界面构建器中,我将ImageView的自动调整大小设置为自动填充屏幕。 在我的ViewController中,我添加了一个方法来检测方向的变化,并根据iPad是竖屏还是横屏设置适当的图像:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
myImageView.image = [UIImage imageNamed:@"image-landscape.png"];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
myImageView.image = [UIImage imageNamed:@"image-portrait.png"];
} }
UIInterfaceOrientationIsLandscape
&& UIInterfaceOrientationIsPortrait
的好例子。 - james_womack我花了一些时间才理解这个概念。我不想创建相同的纵向和横向图像。关键在于,CGAffineTransformMakeRotation
从您的UIImageView或任何UIView的原始状态旋转。这假设您的背景图像具有方向性。例如,您希望您的UIImageView保持不动,而其他对象对正常方向更改事件作出反应。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
backgroundImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
backgroundImage.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}
else {
backgroundImage.transform = CGAffineTransformMakeRotation(0.0);
}
}
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
imageView.frame = self.view.bounds;
imageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
iimageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
虽然Salah的答案看起来没问题,但我认为你可以在这里做两个改进:
在这个函数中设置背景图片:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
duration (NSTimeInterval)duration
如果你在didRotateFromInterfaceOrientation函数中进行更改,那么当你完成iPad旋转并且从两个背景图像之间进行过渡时,你将会更改背景图像一次,而且过渡不会很平滑:你会清楚地看到新的背景图像在旋转结束时弹出。
改进设置myImageView.image值的方法:
_myImageView.image = UIInterfaceOrientationIsLandscape(interfaceOrientation) ? [UIImage imageNamed:@"image-landscape.png"] : [UIImage imageNamed:@"image-portrait.png"];
我实际上会在docchang的代码中添加另一个分支,因为当iPad旋转到倒立的纵向时,它使用了正常的纵向图像,这可能看起来有点奇怪。 我添加了,
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2);
}
else
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
imageBackgroundView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}
else
if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI);
}
else
{
imageBackgroundView.transform = CGAffineTransformMakeRotation(0);
}
有趣的是,程序员在思考超越框架(在这种情况下确实如此)时往往会遇到困难。
试试这个方法(这是我自己解决问题的方法)。
完成。
关键在于,你应该创建一个正方形图像(就像我上面说的,超越框架)。