【问题标题】:iOS 4.2 How to Display a different image on orientation change?iOS 4.2 如何在方向改变时显示不同的图像?
【发布时间】:2011-07-30 04:19:41
【问题描述】:

iOS4.2 当我的应用程序启动时,我会显示一个在我的 web 视图加载之前可见的图像。在 webview 加载后,该图像被设置为隐藏。如何执行以下操作: 如果是纵向,则显示 DefaultPortrait.png 如果是横向则显示 DefaultLandscape.png?

如果设备是纵向的,则显示

self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

如果设备是横向的,则显示

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 480f, 320.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2L.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

【问题讨论】:

  • 试试我的方法。将 viewDidRotateMethod 放入您的代码中。每当设备旋转时都会调用此函数..

标签: iphone uiimageview ios-4.2 uiinterfaceorientation uideviceorientation


【解决方案1】:

您可以使用didRotateFromInterfaceOrientation 来实现您的代码。现在,当方向将从纵向更改时,实现您想要在设备处于横向模式时显示的代码,反之亦然。希望它能解决你的问题。

    - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {

        // your method for landscape..

    }
    if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {

        //your method for portrait
    }
}

【讨论】:

  • 谢谢,我选择不做我原本打算做的事情。
【解决方案2】:

您可以在开始时使用它来获取当前方向:

   UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation] 

然后使用这个:

 if ((currentOrientation == UIInterfaceOrientationPortrait) || (currentOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

    }
   else if ((currentOrientation == UIInterfaceOrientationLandscapeRight) || (currentOrientation == UIInterfaceOrientationLandscapeLeft)) {
self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
}

【讨论】:

  • earthHappens.com/eHap.html 我将 myviewController .h .m 文件放在我的服务器上。我没有收到错误代码,但是当方向改变时我的图像没有改变。我被难住了
  • 谢谢,我选择不做我原本打算做的事情。
【解决方案3】:

如果您想在两种模式下显示相同的图像,那么首先您在应用程序委托文件中声明一个布尔类型变量并在应用程序委托 .m 文件中定义。 IF 纵向模式变量为 true,横向则为 false。现在在

_(void)viewDidLoad{
              if (Boolean variable == TRUE ){
                                         //display portrait mode }
                 else 
                      //display landscape mode

}

【讨论】:

  • 2 张不同的图片 Default2.png 和 Default2L.png
  • 纵向显示 Default2.png,横向显示 Default2L.png。
  • earthHappens.com/eHap.html 我将 myviewController .h .m 文件放在我的服务器上。我没有收到错误代码,但我的图像在方向发生变化时没有改变。我被难住了
  • 在 app delegate.h 文件中你声明一个布尔类型变量,你检查你的设备是否可以检查方向变化
  • 在 appdelegate.h 文件中声明为 Bool IsLandscape;在 app delegate.m 文件中你合成它。在旋转函数中,你定义为 if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) { IsLandScape = False; } if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) { IsLandScape=TRUE; } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
相关资源
最近更新 更多