2012-03-14 105 views
0

在我的應用程序我有一個家有幾個按鈕。每個按鈕打開一個單獨的視圖。在每個視圖中,如果我將設備放在橫向上,則會顯示幫助視圖。一切工作正常,除了在任何視圖我,如果我把iPhone放置在桌子上(我不知道如何更好地...)應用程序從該視圖中退出並返回到第一個視圖,家。 這裏是我的代碼:如何攔截iPhone旋轉

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" 
object:nil]; 
[self performSelector:@selector (ritardo) withObject:nil afterDelay:5.0f]; 
} 

-(void)orientationChanged:(NSNotification *)object{ 
UIDeviceOrientation deviceOrientation = [[object object] orientation]; 

if (deviceOrientation == UIInterfaceOrientationPortrait) 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    self.view = self.portraitView; 
} 
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation 
== UIInterfaceOrientationLandscapeRight) 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    self.view = self.landscapeView; 
} 
[UIView commitAnimations]; 
[self dismissModalViewControllerAnimated:NO]; 
} 

-(void) ritardo { 
ruota.image = [UIImage imageNamed:@"RuotaPerAiuto.png"];  
} 

- (void)viewDidUnload 
{ 
[self setPortraitView:nil]; 
[self setLandscapeView:nil]; 
[self setRuota:nil]; 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: 
(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
} 
} 
@end 

嗨希望你能幫助我

編輯: 我修改了 - (空)orientationChanged:(NSNotification *)對象{這樣:

-(void)orientationChanged:(NSNotification *)object{ 
UIDeviceOrientation deviceOrientation = [[object object] orientation]; 
if (deviceOrientation == UIDeviceOrientationFaceUp)// || deviceOrientation == 
UIDeviceOrientationFaceDown) 
{ 
    return; 
} 
if (deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == 
UIInterfaceOrientationPortraitUpsideDown) 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    self.view = self.portraitView; 
} 
else 
if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation == 
UIInterfaceOrientationLandscapeRight) 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    self.view = self.landscapeView; 
} 
[UIView commitAnimations]; 
[self dismissModalViewControllerAnimated:NO]; 
} 

現在,如果我在肖像和iPhone獲取faceUp位置的作品不錯。但是,當我從faceUp改變位置到人像時,它會回到家裏...

回答

1

問題是,UIDeviceOrientationDidChangeNotification將獨立於shouldAutorotateToInterfaceOrientation而被解僱。並且在方法orientationChanged:中不處理UIInterfaceOrientationPortraitUpsideDown,UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown的情況。因此,當設備被旋轉到這些方向之一,你的代碼就相當於:

-(void)orientationChanged:(NSNotification *)object 
{ 
    [UIView commitAnimations]; 
    [self dismissModalViewControllerAnimated:NO]; 
} 

所以,你應該刪除該行:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

,把你的方法willRotateToInterfaceOrientation代碼:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) { 
     self.view = self.portraitView; 
    } else { 
     self.view = self.landscapeView; 
    } 
    [UIView commitAnimations]; 
    [self dismissModalViewControllerAnimated:NO]; 
} 

編輯

如果你想保留orientationChanged,然後修改它如下:

-(void)orientationChanged:(NSNotification *)object{ 
    UIDeviceOrientation deviceOrientation = [[object object] orientation]; 
    if (deviceOrientation == UIInterfaceOrientationPortrait) { 
     [UIView beginAnimations:@"View Flip" context:nil]; 
     [UIView setAnimationDuration:0.5f]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
     self.view = self.portraitView; 
     [UIView commitAnimations]; 
     [self dismissModalViewControllerAnimated:NO]; 
    } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [UIView beginAnimations:@"View Flip" context:nil]; 
     [UIView setAnimationDuration:0.5f]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
     self.view = self.landscapeView; 
     [UIView commitAnimations]; 
     [self dismissModalViewControllerAnimated:NO]; 
    } 
} 
+0

感謝您的回答。我只是根據你的建議修改了orientationChanged方法,但它仍然是一樣的。也許我無法解釋我想要處理的iPhone的位置:它可能是faceUp o像這樣的東西? – Enzoses 2012-03-14 10:15:44

+0

好的!現在它工作得很好,謝謝! – Enzoses 2012-03-14 10:38:09

+0

不...我說話太早...現在它回到首頁時,iPhone的位置從faceUp返回到肖像:(我通過添加一個if(deviceOrientation == UIDeviceOrientationFaceUp)修改了orientationChanged方法//也嘗試過|| deviceOrientation == UIDeviceOrientationFaceDown){return;} – Enzoses 2012-03-14 11:03:31