2012-03-14 9 views
0

私のアプリにはいくつかのボタンが付いています。各ボタンは別々のビューを開きます。各ビューで、デバイスを横長にするとヘルプビューが表示されます。私がiPhoneをテーブルの上に置くように置くと(私はeplainの方がわかりません...)、アプリケーションはそのビューから抜け出し、最初のビューに戻ります。ホーム。 は、ここに私のコードです: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 

こんにちはあなたは私に役立つことを願って

EDIT: は私が修正 - (ボイド)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

問題は、shouldAutorotateToInterfaceOrientationから独立して発砲されるということです。また、方法orientationChanged:UIInterfaceOrientationPortraitUpsideDown,UIDeviceOrientationFaceUpおよびUIDeviceOrientationFaceDownのケースは処理しません。ですから、行削除する必要があり

-(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することができますか? – Enzoses

+0

今それは素晴らしい、ありがとう! – Enzoses

+0

いいえ...私はあまりにも早く話しました...今、iPhoneの位置がfaceUpからPortraitに戻るときにHomeに戻ります:(私はif(deviceOrientation == UIDeviceOrientationFaceUp)//を追加してorientationChangedメソッドを変更しました。 || deviceOrientation == UIDeviceOrientationFaceDown){return;} – Enzoses

関連する問題