【问题标题】:iPad - Change image depending on device orientation (Portrait or Landscape)iPad - 根据设备方向(纵向或横向)更改图像
【发布时间】:2012-08-29 20:04:42
【问题描述】:

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

我找到了 this page,这是我的代码的基础。

我的代码如下所示。 (这都包含在我的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

它仍然无法正常工作:(

【问题讨论】:

  • 你在加载过程中改变了 iPad 的方向吗?
  • 没有。 iPad 处于横向,然后启动应用程序。

标签: objective-c ios ipad device-orientation


【解决方案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];
}

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

【讨论】:

  • 当我在 Xcode 中实现此代码时,在行 isPortraitView = YES; 上出现错误“使用未声明的标识符“isPortraitView””我需要做什么?
  • 在if语句上方添加BOOL isPortraiView;
  • 我会将它添加到 .h 文件中,使其在类范围内可用。
  • 更新了我的答案。我回顾了我的代码,发现我刚刚在实现之上声明了它,因此其中的所有方法都可以访问该变量。这有帮助吗?
  • 好的...所以我把它放在我的viewDidLoad 方法之前。启动应用程序时这会起作用吗?例如,如果可能,我希望此代码在应用程序出现之前运行。如果有,怎么做?
【解决方案2】:

很高兴您知道了,但看在上帝的份上,请删除用于方向的硬编码数值!为此声明了枚举:

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

您可能应该检查您的 viewController 的 interfaceOrientation 而不是设备方向。

另外,请查看以下宏:

UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)

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

UIInterfaceOrientationIsLandscape(self.interfaceOrientation) {
    // show landscape image
} else {
    // show portrait image
}

【讨论】:

  • 我尝试了类似您的代码的方法,但由于某种原因它不起作用......?因此,除非您可以按照我的回答所示实施它,否则我会坚持使用我的:((我是新手)
【解决方案3】:

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

//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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多