开发者

Disallow rotating on splash view in iOS

I have a splash screen I fade the alpha on from 100 to 0. I want shouldAutorotateToInterfaceOrientation to return YES only when the alpha is 0. I cannot get this code to work:

if ( splashView.alpha == 0开发者_JS百科 )
{
    return YES;
} else {
    return NO;
}

Instead, splashView.alpha always returns null. Any ideas?


When you set the alpha to zero turning around and asking the view for its alpha will return zero. Even though the animation is not finished (the difference between the modelLayer and presentationLayer in Core Animation if you want to understand what's going on).

To get what you are looking for something like this should work;

- (BOOL)shouldAutorotateToInterfaceOrientation:(...) {
  BOOL shouldAutoratate = NO;
  if(self.splashViewFaded) {
    shouldAutorotate = YES;
  }
  return shouldAutorotate;
}

Then in your appDidFinishLaunching method (or wherever makes sense) do something like this;

self.viewController.splashViewFaded = NO;
[UIView animateWithDuration:0.25 // or whatever
                      delay:0.0 // or whatever
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^() {
                                self.splashView.alpha = 0.0;
                            }
                 completion:^() {
                                self.viewController.splashViewFaded = YES;
                                [self.splashView removeFromSuperview];
                 }];

Word to the wise, I've typed this from memory so it probably won't compile, if you copy and paste please go read the docs.


During the animation, the value returned from the alpha property will not necessarily match the instantaneous alpha of the view. And further, once the alpha reaches 0 there is no guarantee that the system will re-check the result of shouldAutorotateToInterfaceOrientation:, so your interface may wind up stuck in portrait until the device is turned.

First, consider whether this is really the best plan. If the user is holding the device in landscape or upside-down, is it really the best experience to display the splash screen sideways or upside-down with respect to how they're looking at it. Although I could see it on an iPhone if your "splash screen" matches Default.png, as that is always displayed in portrait mode too.

But if you must, a better plan would be for your willAnimateRotationToInterfaceOrientation:duration: method to apply a counter-rotation to the splash screen view, to keep the splash screen "upright" no matter what the orientation of the rest of the app. See Keeping controls in landscape mode but allowing uitabbar to switch for details.


The alpha value should be between 1.0 and 0.0, so I'm guessing that might be your problem.


You should just use block animations:

[UIView animateWithDuration:2.0 
                 animations:^{
                     splashView.alpha = 0.0;
                 }
                 completion:^(BOOL finished){
                     return YES;
                 }];

This will run the animation block and once completed, return YES for your method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜