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

发布于 2024-11-27 02:28:52 字数 871 浏览 2 评论 0原文

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

iOS4.2 When my app launches I display an image that is visible until my webview loads. After the webview loads that image is set to hidden. How do I do the following: If portrait, then display DefaultPortrait.png if Landscape the display DefaultLandscape.png?

if Device is Portrait then display

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

if Device is landscape then display

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-12-04 02:28:52

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

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

        // your method for landscape..

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

        //your method for portrait
    }
}

You can use the didRotateFromInterfaceOrientation to implement your code. Now here when the orientation will be changed from portrait implement the code u wanna display when the device is in landscape mode and vice versa. Hope it solves your problem.

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

        // your method for landscape..

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

        //your method for portrait
    }
}
葬花如无物 2024-12-04 02:28:52

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

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

You can use this at the beginning to get your current orientation:

   UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation] 

and then use this:

 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];
}
纵性 2024-12-04 02:28:52

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

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

}

If you want to display same image in both mode then first u declare a Boolean type variable in app delegate file and define in app delegate .m file. IF portrait mode the variable true and when landscape then false. Now In

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文