如何在 iOS 上旋转自定义启动屏幕?

发布于 2024-10-06 12:47:02 字数 909 浏览 4 评论 0原文

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

如何启动应用程序,以便启动屏幕像我的应用程序一样在横向模式之间旋转?

我正在使用以下代码:

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

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

My splash screen is working, but my app works on landscape mode, and the splash screen shows in the default portrait mode.

How can I start the app so that the splash screen rotates between landscape modes like my app?

I'm using the following code:

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

and for the splash screen

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

But how can I put the rotate inside the display screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂人 2024-10-13 12:47:02

啊哈。如果您想显示自己的启动屏幕,您应该为此创建一个特殊的视图控制器,您已经这样做了。我认为您可以简化自动旋转查询代码:

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

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

Aha. If you want to display your own splash screen, you should create a special view controller for that, which you already did. I think you can simplify the autorotation query code:

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

Now you have to think about the splash screens for different orientations. Do you have a separate splash image for landscape and portrait? If yes, you can do something like this:

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

There are two utility functions called, you can stuff these into a separate file:

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

It’s a bit messy, I’d be interested in simpler solution myself.

╰ゝ天使的微笑 2024-10-13 12:47:02

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

更新:

通过向在 -startupImageWithOrientation: not 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];
}

@zoul - loving this solution so far. however, if/when that view has any subviews - they don't show up. any ideas?

update:

fixed this issue by adding a subview to the UIView created in -startupImageWithOrientation: not self.view.

- (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];
}
靖瑶 2024-10-13 12:47:02

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

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

一般来说,1024x768 图像意味着纵向,而 768x1024 图像意味着横向。

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

将所有正常的 viewController 加载到窗口中,然后将您的“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);
}

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

The resolution of your defult.png can effect your apps orientation if you have locked orientations.

For example if a 1024x768 splash image is used and the initial view doesn’t support portrait viewing through orientation locks this can cause visual UI objects to appear off screen (especially when animation is involved) as the view will try and present itself in a portrait configuration even though the device may be in landscape.

Generally, 1024x768 images imply portrait while 768x1024 images imply landscape.

If this isn't enough, or your wanting to seamlessly go from the initial default image into e.g a login screen, then you can use a viewcontroller to 'continue' the splash .

Load all the normal viewControllers into the window, then put your 'splash' viewController in then in your splashController use shouldAutorotateToInterfaceOrientation method to set the right image (on an 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);
}

The configuration of the images i have used may not suit you, so some experimentation might be needed.

墨洒年华 2024-10-13 12:47:02
- (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:

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

    }

note that you should return YES the method shouldAutorotateToInterfaceOrientation:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文