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

Привет надеюсь, что вы можете помочь мне

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 на портрет, он возвращается к дому ...

ответ

1

Проблема в том, что UIDeviceOrientationDidChangeNotification будет уволен независимо от shouldAutorotateToInterfaceOrientation. И вы не обрабатываете случаи UIInterfaceOrientationPortraitUpsideDown, UIDeviceOrientationFaceUp и UIDeviceOrientationFaceDown по методу orientationChanged:. Таким образом, когда устройство поворачивается к одному из этих направлений, ваш код эквивалентен:

-(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, с которой я бы хотел справиться: может ли это выглядеть так? – Enzoses

+0

Хорошо! Теперь он отлично работает, спасибо! – Enzoses

+0

Нет ... Я говорил слишком рано ... Теперь он возвращается к дому, когда позиция iPhone возвращается из faceUp в Portrait :(. Я изменил метод orientationChanged, добавив if (deviceOrientation == UIDeviceOrientationFaceUp) // также пытался с || deviceOrientation == UIDeviceOrientationFaceDown) {return;} – Enzoses

Смежные вопросы