我試圖更改旋轉視圖,因爲我的視圖必須明顯不同於縱向與橫向。現在我正在使用的代碼工作一次,然後應用程序在嘗試旋轉時凍結。無論哪個方向都沒有什麼不同。例如:如果我在風景和旋轉到肖像一切都很好,直到我旋轉回風景然後它凍結,並沒有什麼。在iPad上UIInterfaceOrientation期間更改UIViews
這裏是我使用來實現這一
在我「的viewDidLoad」方法的代碼
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
然後我把這個用於旋轉:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft) ||
(orientation == UIDeviceOrientationLandscapeLeft))
{
// present the other viewController, it's only viewable in landscape
[self.view addSubview:landScapeView];
}
if ((orientation == UIDeviceOrientationLandscapeRight) ||
(orientation == UIDeviceOrientationLandscapeRight))
{
// present the other viewController, it's only viewable in landscape
[self.view addSubview:landScapeView];
}
else if ((orientation == UIDeviceOrientationPortrait ||
(orientation == UIDeviceOrientationPortrait))
{
// get rid of the landscape controller
[self.view addSubview:portrait];
}
else if ((orientation == UIDeviceOrientationPortraitUpsideDown ||
(orientation == UIDeviceOrientationPortraitUpsideDown))
{
// get rid of the landscape controller
[self.view addSubview:portrait];
}
}
請在下次提問時花時間格式化您的代碼,因爲這樣可以更容易閱讀,因此您可以得到更好的答案。另外,我不確定爲什麼你要在每個'if/else'條件中檢查'orientation'變量兩次以獲得相同的值;你應該簡化這個代碼。 –
對不起,我通常......遲到了。感謝您修復它! – FreeAppl3