2010-11-29 31 views
12

觸發在這裏運行時,iOS設備方向變化法的方式進行?的方法通過改變方位

我想只改變屏幕上的一些物體的方向,而不是別人。

代表應怎樣使用等

乾杯 -A新手

回答

32

取決於當你想做出反應:

如果旋轉從UIViewController中之前,覆蓋

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
// do something before rotation 
} 

如果你想旋轉後執行的東西:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
// do something after rotation 
} 

參考:

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/willRotateToInterfaceOrientation:duration

+3

這些方法已被棄用。 – 2015-01-01 12:50:19

+1

在IOS 8.0 – onmyway133 2016-07-25 19:29:31

0

willRotateToInterfaceOrientation:是你可能看方法。閱讀那一篇。

0
6

帖子UIDeviceOrientationDidChangeNotification

UIApplication帖子UIApplicationWillChangeStatusBarOrientationNotificationUIApplicationDidChangeStatusBarOrientationNotification,每種相關的委託回調。

UIViewController接收如果視圖控制器是由一個窗口管理控制器層次結構中的UIDevice通知觸發幾個方向相關的調用。

如果你已經在使用一個UIViewController,實現一些的方向相關的方法,否則報名參加的UIDevice通知。最重要的UIViewController方法是shouldAutorotateToInterfaceOrientation因爲如果回報NO別人不叫。

+0

棄用我寧願作爲觀察者以接收所述取向通知進行註冊。所以我投了這個答案。 – AechoLiu 2010-11-29 10:18:21

0

我的沙盒應用: https://github.com/comonitos/programatical_device_orientation

的解決方案是簡單:

接口

(H文件):

BOOL rotated; 

在執行(M檔):

  1. 改寫

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 返回旋轉; }

2調用[自我設置]

-(void) setup 
{ 
    rotated = YES; 
    [[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft]; 
    rotated = NO; 
} 
0

只需使用此代碼段檢查方向變化。

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
    if UIDevice.currentDevice().orientation.isLandscape.boolValue { 
     print("Landscape") 
    } else { 
     print("Portrait") 
    } 
}