0

在我的viewDidLoad方法中,我正在应用一个“加载图像”,它会webView在完成加载之前出现。我想根据 iPad 所处的方向(纵向或横向)指定不同的加载图像。

我找到了这个页面,这是我的代码的基础。

我的代码如下所示。(这都包含在我的viewDidLoad方法中)。目前这只适用于纵向模式。知道为什么我的横向代码没有被调用吗?请帮忙!

//detect if iPad    
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//if in Portrait Orientation
            if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
                loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
                loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
                                                    nil];
            }
//if in Landscape Orientation
            if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
               || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
                loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
                loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
                                                    nil];
            }

        }

更新

我知道您应该能够使用图像文件名修饰符,以便设备自动提取正确的图像。我尝试将 webView-load-image.png 放入我的应用程序图像文件夹中,并使用所有正确的扩展名(并-Portrait~ipad从文件名中删除):

  • webView-load-image~iphone.png
  • webView-load-image@2x~iphone.png
  • webView-load-image-Landscape~ipad.png
  • webView-load-image-Landscape@2x~ipad.png
  • webView-load-image-Portrait~ipad.png
  • webView-load-image-Portrait@2x~ipad.png

它仍然无法正常工作:(

4

3 回答 3

1

我回顾了我不久前开发的一个应用程序,我正在做类似的事情。这就是我让它工作的方式......

我创建了一种方法来处理方向的变化......

BOOL isPortraitView = YES;

@implementation MyViewController
{
    // a bunch of code...

    - (void)setOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
        {
            isPortraitView = YES;

            // do other stuff
        }
        else
        {
            isPortraitView = NO;

            // do other stuff
        }
    }

    // other code...
}

它接受一个UIInterfaceOrientation值,因此您不必使用状态栏方法(如果我没记错的话,该方法不是最好的/最可预测的)。我还存储了一个类级别的布尔值以供快速参考。然后我在方法上调用它willRotateToInterfaceOrientation

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self setOrientation:toInterfaceOrientation];
}

这也可以处理加载过程中的方向变化。

于 2012-08-29T20:45:41.173 回答
1

很高兴你明白了,但看在上帝的份上,删除方向的硬编码数值!为此声明了枚举:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

你可能应该检查你的 viewControllerinterfaceOrientation而不是设备方向。

此外,请查看以下宏:

UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)

您的代码应该看起来更像:

UIInterfaceOrientationIsLandscape(self.interfaceOrientation) {
    // show landscape image
} else {
    // show portrait image
}
于 2012-08-30T15:01:33.373 回答
0

我自己想通了。如果其他人遇到同样的问题,这对我有用:

//ViewController.h
@interface ViewController : GAITrackedViewController {
    // ...other code
    UIImage *loadingImage;
}

@property(nonatomic, strong) UIImageView *loadingImageView;


//ViewController.m
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        ...bunch of other code....

    // Subscribe to device orientation notifications, and set "orientation" - will return a number 1-5, with 5 being "unknown".
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

    //**************** Start Add Static loading image  ****************/
        //if iPhone
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        loadingImage = [UIImage imageNamed:@"webView-load-image~iphone.png"];
        loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
            loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                [UIImage imageNamed:@"webView-load-image~iphone.png"],
                                                nil];
        }

       if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
           if(orientation == 3 || orientation == 4) {
               loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
               loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
               loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                   [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
                                                   nil];
               NSLog(@"Set webView-load-image iPhone Landscape");

           }
           if(orientation == 5) {
               NSLog(@"Set webView-load-image UNKNOWN");
           }
           if(orientation == 1) {
                loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
                loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
                                                    nil];
                NSLog(@"Set webView-load-image iPad Portrait");
            }


        }

        [self.view addSubview:loadingImageView];
        // End Add Static loading image
于 2012-08-30T14:49:05.780 回答