开发者

Very Intermittent Orientation on Device & Simulator

I've noticed that I'm getting very intermittent orientation on my device & the simulator.

I have a modal v开发者_JAVA技巧iew controller that I present, and that is the only thing in my app which supports rotation.

If I launch the app in portrait without moving the device, open the modal VC and then rotate the device, it usually works. However sometimes if I open the app holding the device in landscape, then rotate to portrait, launch the VC and then rotate the device, no rotation occurs. It seems very intermittent. Sometimes if I launch the app in portrait mode and then open the VC and rotate the device, nothing happens, and until I quit and relaunch it no orientation occurs in the app.

It's strange because 50% of the time it works! Whenever I launch it through Xcode and set breakpoints in shouldAutorotateToInterfaceOrientation it always works!

Anyone ever had this or know what's going on?


Since as you mentioned "intermittent", i would say it has something to do with what are you doing after the rotation.

To find the bug, i suggest removing any code after the rotation happens. Comment out any network activities or OpenGL operations. Could also help to close XCode and reopen it.

If nothing helps, i would make a new project and start moving the files one by one and test.


I've had similar challenges getting autorotation to work properly for view controllers whose parent view controllers don't autorotate, although most of my experience has related to wrangling UINavigationController as opposed to modal view controllers. Still, I'd recommend trying the following:

  1. Call presentModalViewController: on the top level view controller in your hierarchy rather than a deeper viewController.

  2. If that doesn't solve it, try subclassing your top level view controller, and overriding its shouldAutorotateToInterfaceOrientation: to look something like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (self.modalViewController) {
        return [self.modalViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


Does calling

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

when you show your rotation-aware view help? (and possibly turning it off again when you dismiss it?)

The other thing I would look for is suspicious calls to [UIWindow makeKeyWindow] or [UIResponder becomeFirstResponder].

I'd also sign up for UIDeviceOrientationDidChangeNotification events and make sure that the Modal view got a call to [UIViewController shouldAutorotateToInterfaceOrientation:] for each event you receive.


I've finally discovered what the problem was!

It turns out that only the very first UIView you add to your UIWindow will get asked whether or not to rotate. Any subsequent views don't receive this message. My app was using another view to animate away from the Default.png that is displayed on launch, and this was added to the window first!


Even if you try to launch your application in landscape, your phone will launch it in portrait and then go into landscape depending in which position your phone is.

I don't know what code you are using to accomplish this. Below is one which is taken from Apple code snippets. See if this can help.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {    
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);  
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = 
        CGAffineTransformMakeRotation(degreesToRadian(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜