如何在iOS上旋转自定义初始屏幕?

时间:2010-12-07 06:31:50

标签: iphone ipad ios autorotate uiinterfaceorientation

我的启动画面工作正常,但我的应用程序在横向模式下工作,启动画面以默认纵向模式显示。

如何启动应用以使启动画面在我的应用程序等横向模式之间旋转?

我正在使用以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
    interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    return YES;
else {
    return NO;  }
}

和启动画面

-(void)displayScreen { 
UIViewController *displayViewController=[[UIViewController alloc] init];
displayViewController.view = displaySplashScreen;
[self presentModalViewController:displayViewController animated:NO];
[self performSelector:@selector(removeScreen) withObject:nil afterDelay:3.0];
} 
 -(void)removeScreen
{   [[self modalViewController] dismissModalViewControllerAnimated:YES];
}

但是如何将旋转放在显示屏内?

4 个答案:

答案 0 :(得分:4)

啊哈。如果要显示自己的启动画面,则应为此创建一个特殊的视图控制器。我认为你可以简化自动旋转查询代码:

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) foo
{
    return YES; // all interface orientations supported
}

现在你必须考虑不同方向的闪屏。您是否有单独的风景图像和肖像?如果是,您可以这样做:

- (UIView*) startupImageWithOrientation: (UIInterfaceOrientation) io
{
    UIImage *img = [UIImage imageNamed:[NSString
        stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]];
    UIView *view = [[UIImageView alloc] initWithImage:img];
    [view setFrame:[[UIScreen mainScreen] applicationFrame]];
    return [view autorelease];
}

- (void) loadView
{
    self.view = [self startupImageWithOrientation:self.interfaceOrientation];
}

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) io
    duration: (NSTimeInterval) duration
{
    self.view = [self startupImageWithOrientation:io];
    self.view.transform = CGAffineTransformFromUIOrientation(io);
}

有两个实用程序函数被调用,您可以将它们填充到一个单独的文件中:

NSString *UIInterfaceOrientationName(UIInterfaceOrientation io)
{
    return UIInterfaceOrientationIsPortrait(io) ? @"Portrait" : @"Landscape";
}

CGAffineTransform CGAffineTransformFromUIOrientation(UIInterfaceOrientation io)
{
    assert(io <= 4);
    // unknown, portrait, portrait u/d, landscape L, landscape R
    static float angles[] = {0, 0, M_PI, M_PI/2, -M_PI/2};
    return CGAffineTransformMakeRotation(angles[io]);
}

这有点乱,我对自己更简单的解决方案感兴趣。

答案 1 :(得分:3)

@zoul - 到目前为止爱这个解决方案。但是,如果/当该视图有任何子视图时 - 它们不会显示。任何想法?

更新

通过在-startupImageWithOrientation: 而不是 self.view中创建的UIView添加子视图来解决此问题。

- (UIView *)startupImageWithOrientation:(UIInterfaceOrientation)io{
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]];
    UIView *aView = [[UIImageView alloc] initWithImage:img];
    [aView setFrame:[[UIScreen mainScreen] applicationFrame]];

    // define the version number label
    self.versionNumberLabel_iPadSplashScreen.text = [NSString stringWithFormat:@"Version %@", 
                                                                           [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]; 

    [aView addSubview:self.versionNumberLabel_iPadSplashScreen];

    return [aView autorelease];
}

答案 2 :(得分:1)

如果您有方向锁定,defult.png的分辨率会影响您的应用方向。

例如,如果使用1024x768启动图像并且初始视图不支持通过方向锁定进行纵向查看,则可能导致可视UI对象出现在屏幕外(特别是涉及动画时),因为视图将尝试呈现自身即使设备可能处于横向状态,也可以进行纵向配置。

一般来说,1024x768图像暗示肖像,而768x1024图像暗示风景。

如果这还不够,或者您想要从初始默认图像无缝地转到例如登录屏幕,那么您可以使用视图控制器来“继续”启动。

将所有普通的viewControllers加载到窗口中,然后将你的'splash'viewController放入splashController中使用shouldAutorotateToInterfaceOrientation方法设置正确的图像(在ImageViewController上):

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.

 switch ([[UIDevice currentDevice] orientation])
 {
  case UIInterfaceOrientationLandscapeRight:
   splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]];
   break;
  case UIInterfaceOrientationPortrait:
   splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]];
   break;
  default:
   splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default2"] ofType:@"png"]];
   break;
 } 

   return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

我使用的图像配置可能不适合您,因此可能需要进行一些实验。

答案 3 :(得分:0)

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    {
        // take action here , when phone is "Portrait" 

    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        action 4 LandscapeLeft

    }
    else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        //PortraitUpsideDown

    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        //LandscapeRight

    }

请注意,您应该使用方法shouldAutorotateToInterfaceOrientation:

返回YES