2012-01-11 99 views
0

我正在做一個應用程序,我改變按鈕佈局取決於如果用戶在縱向與橫向模式中移動他的iphone。我認爲把我的代碼的最佳地點是在最好的方法來檢測iphone方向

-(BOOL) shouldautorotatetointerfaceorientation 

但也有與

  1. 當頁面加載shouldautorotatetointerfaceorientation兩大問題被稱爲至少3倍。我只想執行一次我的代碼。

  2. 即使當我從viewController1(page1)移動到viewController2(page2)與Modal segue,我旋轉我的iphone在page2它仍然調用自動代碼,這是我的viewcontroller1。這很奇怪。

總之,什麼是最好的地方把我的方向爲基礎的圖標?

回答

5

方法

-(BOOL) shouldautorotatetointerfaceorientation 

的唯一目的是確定哪些取向支持。

如果你希望做旋轉的自定義操作,那麼你想實現一個或兩個方法的

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration 
+0

...或'willAnimateRotationToInterfaceOrientation',這往往是非常有用的,因爲你把這個方法中的任何代碼(如適用)動畫也很容易。 – lxt 2012-01-11 21:34:13

+0

我只是用這個和apprently我需要添加[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate :) name:@「UIDeviceOrientationDidChangeNotification」object:nil];隨着didRotate函數和didRotate我會知道用戶是否在縱向或橫向模式下移動設備? – 2012-01-11 21:42:13

+0

@SamBudda你不需要註冊上面列出的方法的默認值。但是,如果您想要爲任何設備方向更改觸發自定義方法'didRotate',則應添加您提到的通知。 – PengOne 2012-01-11 21:51:47

0

UIDevice.orientation;

但是,你還是應該正確地實現從通知用戶的旋轉。如果你想避免不必要地多次運行你的代碼,只要記住最後一個方向是什麼,並且只有在代碼發生變化時才執行。

+2

請注意,設備方向與界面方向不同。 – PengOne 2012-01-11 21:35:57

+0

@PengOne好點! – 2012-01-11 21:38:53

1

您需要創建UIView的子類並覆蓋-(void)layoutSubviews。此消息將在您更改方向或設置setNeedsToDisplay時調用。你將獲得動畫改變你的子視圖的位置。
獲取當前定向[[UIApplication sharedApplication] statusBarOrientation]