2011-06-14 131 views
12

我知道如何允許/不允許定向使用有沒有辦法強制在iPhone中改變界面方向?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

但是旋轉,我有問題,現在哪裏的手機可能會在縱向(或顛倒),在某些時候,我想旋轉就好像用戶旋轉到風景一樣。在這一點上,我不希望autorotate工作了。我想強制界面方向保持橫向。有沒有辦法做到這一點?我大概可以找出關閉自動旋轉的手段,但首先迫使旋轉,我不知道該怎麼做。

這裏就是我想要做的事:

  • 的應用旋轉到任何和所有的方向。一切正常。

  • 發生事件。

  • 現在自動旋轉只適用於landscapeleft和landscaperight。此外,如果用戶在肖像或portraitupsidedown,我以編程方式旋轉到landscaperight鎖定用戶到該方向。

只是爲了讓事情變得棘手,我想強制旋轉前UIAlertView(我知道如何做到這一點),彈出,讓用戶知道發生了什麼事情,我想篩選alertView背後旋轉但不是alertView。這甚至有可能嗎?

謝謝。

回答

-2

是的,你可以做到這一點。你要做的是什麼 - (空)viewWillAppear中方法簡單地包括下面的代碼

if (self.interfaceOrientation == <current orientation>) { 
     [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)<to which orientation you want to change.>]; 
    } 

它會解決你的問題

+2

此API已隱藏,UIDevice實例的orientation屬性爲只讀,所以在Apple審閱過程中可能會導致您遇到問題。 – 2012-10-03 13:29:37

+0

對於<你想改變的方向>,UIInterfaceOrientation的類型不能被強制爲id。什麼是解決方法? – schellsan 2013-01-15 23:42:00

0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
    } 


    // orientation view swapping logic 
- (void)didRotate:(NSNotification *)notification { 


    UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation]; 
    if (newOrientation != UIDeviceOrientationUnknown || newOrientation != UIDeviceOrientationFaceUp || newOrientation != UIDeviceOrientationFaceDown) 
    { 
      orientation = newOrientation; 

    } 

    // Write your orientation logic here 
     if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) 
    { 
    // Clear the current view and insert the orientation specific view. 
     [self clearCurrentView]; 
     [self.view insertSubview:yourLandscapeView atIndex:0]; 

    } else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) 
    { 
      // Clear the current view and insert the orientation specific view. 
      [self clearCurrentView]; 
      [self.view insertSubview:yourPortraitView atIndex:0]; 

    } 
} 

,並添加通知事件

0

你不能。至少不是蘋果允許的方式。自動旋轉被設計爲與整個應用程序相同。您可以製作手動旋轉視圖的代碼,但就UIDevice而言,您將處於相同的方向。

1

您的情況的最佳解決方案是通知用戶將其轉向橫向並暫停程序,直到用戶轉向。然後當用戶旋轉設備時,您可以處理委託方法並開始以橫向模式顯示屏幕。這種方法已經在許多應用商店應用中得到處理,看起來這是獲得應用商店中批准的應用的最佳方式之一。我希望這是有道理的。

0

據蘋果公司稱,如果你不想在設備方向轉動API提供的默認行爲:

您可以接管控制:

  • 您的應用
  • 支持的方向
  • 兩個方向之間的旋轉如何在屏幕上動畫。

我看這個問題的方法是:「你可以控制任何這些東西,但沒有別的」或「你自己也可以只控制這兩件事情。」

我沒有在文檔中看到任何明確告訴你如何「強制」方向改變的文檔。所有這些都是由設備處理的,因此看起來對於持有該設備的人來說更自然和友好。那有意義嗎?

請參閱該文檔在這裏旋轉的變化:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

最後,我會說這是不可能的,進一步的,它不會被蘋果批准。我投票贊成Praveen的回答,因爲我認爲你可能需要考慮一種不同的方法來處理你想要做的事情。

相關問題