5

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

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

我正在使用以下代码:

- (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

4 回答 4

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]);
}

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

于 2010-12-07T10:32:40.000 回答
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];
}
于 2010-12-22T00:17:30.120 回答
1

如果您已锁定方向,则 defult.png 的分辨率可能会影响您的应用程序方向。

例如,如果使用 1024x768 初始图像并且初始视图不支持通过方向锁定进行纵向查看,这可能会导致可视 UI 对象出现在屏幕外(尤其是涉及动画时),因为视图将尝试以纵向显示自己即使设备可能是横向配置。

通常,1024x768 图像意味着纵向,而 768x1024 图像意味着横向。

如果这还不够,或者您希望从初始默认图像无缝进入例如登录屏幕,那么您可以使用视图控制器来“继续”启动画面。

将所有普通 viewController 加载到窗口中,然后将您的“splash”视图控制器放入您的 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);
}

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

于 2010-12-07T10:53:23.400 回答
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

    }

请注意,您应该返回 YES 方法shouldAutorotateToInterfaceOrientation:

于 2010-12-13T07:27:26.837 回答