Reputation: 115
in my app I got a Home with several buttons. Each button opens a separate view. In each view, if I put the device in landscape a help view is shown. Everything works fine except that in whatever view I am, if I put the iPhone like to lay on a table (I don't know how to eplain better...) the app exites from that view and comes back to the first view, the Home. Here's my code:
- (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
Hi hope you can help me
EDIT: I modified the -(void)orientationChanged:(NSNotification *)object{ in this way:
-(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];
}
Now, if I'm in portrait and get the iPhone to faceUp position works good. But when I change position from faceUp to portrait it goes back to the home...
Upvotes: 0
Views: 584
Reputation: 27506
The problem is that UIDeviceOrientationDidChangeNotification
will be fired independently from shouldAutorotateToInterfaceOrientation
. And you don't handle the cases UIInterfaceOrientationPortraitUpsideDown
, UIDeviceOrientationFaceUp
and UIDeviceOrientationFaceDown
in the method orientationChanged:
. So when the device is rotated to one of these orientations, your code is equivalent to:
-(void)orientationChanged:(NSNotification *)object
{
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
So you should remove the lines:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" object:nil];
and put you code in the method 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];
}
Edit
If you want to keep orientationChanged
, then modify it as follows:
-(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];
}
}
Upvotes: 1