iOS 4.2如何在方向更改时显示不同的图像?

时间:2011-07-30 04:19:41

标签: iphone uiimageview ios-4.2 uiinterfaceorientation uideviceorientation

iOS4.2当我的应用程序启动时,我会显示一个在我的webview加载之前可见的图像。在webview加载后,该图像被设置为隐藏。如何执行以下操作:如果是纵向,则在显示DefaultLandscape.png时显示DefaultPortrait.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];

OR

如果设备是横向,则显示

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];

3 个答案:

答案 0 :(得分:3)

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

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

        // your method for landscape..

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

        //your method for portrait
    }
}

答案 1 :(得分: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];
}

答案 2 :(得分:0)

如果要在两种模式下显示相同的图像,则首先在app委托文件中声明一个布尔类型变量,然后在app delegate .m文件中定义。如果是纵向模式,则变量为true,而横向则为false。现在在

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

}