2012-08-29 52 views
0

在我的viewDidLoad方法中,我應用了一個「加載圖像」,該圖像出現,直到webView完成加載。我想指定一個不同的加載圖像,取決於iPad所處的方向(縱向或橫向)。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]; 
      } 

     } 

UPDATE

我知道你應該能夠使用圖像文件名修改器,從而使設備會自動拉正確的圖像。我試圖把網頁瀏覽式的負載image.png在我的應用程序文件夾中的圖像與所有正確的擴展名(從文件名去掉-Portrait~ipad):

,它仍然沒有工作:(

+0

你在加載過程中改變了iPad的方向是什麼? – CoderMarkus

+0

否。iPad處於橫向模式,然後啓動應用程序。 – adamdehaven

回答

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 
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值,所以你不必使用狀態bar方法(如果我沒有記錯的話,那個方法不是最好的/最可預測的)。我還存儲一個類級布爾值以供快速參考。然後我用willRotateToInterfaceOrientation方法調用它。

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

這還可以處理加載過程中的方向更改。

+0

當我在Xcode中實現這個代碼時,在'isPortraitView = YES'行中出現錯誤「使用未聲明的標識符」isPortraitView「」我需要做什麼? – adamdehaven

+0

在if語句之上添加'BOOL isPortraiView;' – Alex

+0

我會將它添加到.h文件中,它可以在類中使用。 – CoderMarkus

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

而且你應該檢查你的viewController的interfaceOrientation,而不是設備的方向。

此外,檢查出下面的宏:

UIInterfaceOrientationIsPortrait(orientation) 
UIInterfaceOrientationIsLandscape(orientation) 

您的代碼應該看起來更像是:

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

我嘗試了一些類似於你的代碼的東西,並且因爲某些原因沒有工作...?所以除非你能像我的答案顯示的那樣執行它,否則我會被困在我的:((我是一個新手) – adamdehaven