1

iOS4.2 当我的应用程序启动时,我会显示一个可见的图像,直到我的 webview 加载。在 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];
4

3 回答 3

3

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

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

        // your method for landscape..

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

        //your method for portrait
    }
}
于 2011-07-30T04:27:02.613 回答
0

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

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

}

于 2011-07-30T05:26:18.330 回答
0

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

   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];
}
于 2011-07-30T04:36:52.823 回答