2015-06-01 26 views
0

我希望我的應用程序在iPhone的方向更改時觸發操作,而不管用戶在應用程序中的位置。在應用程序中運行代碼而不管視圖

我使用

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 


} 

要檢查設備的方向。

我在哪裏放置這段代碼,以便它在方向改變時始終運行?

回答

2

在你的應用程序代理,註冊了UIDeviceOrientationDidChangeNotification通知。

然後在選擇的通知,您就可以執行if檢查。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name: UIDeviceOrientationDidChangeNotification object:nil]; 

    // everything else you had in this method 
} 

- (void)orientationChange { 
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 
    } 
} 
在應用程序委託
相關問題