2012-03-14 111 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