【问题标题】:iOS: Animate splash screen out on top of status bariOS:在状态栏顶部动画启动屏幕
【发布时间】:2013-01-12 17:43:51
【问题描述】:

好的,我的概念是在应用启动后立即添加UIImageView,并对其进行动画处理以伪造启动动画。但是,状态栏会干扰。

我需要一个UIImageView 在所有内容之上,包括状态栏,当它消失时,应用程序与状态栏一起显示。因此,将状态栏设置为最初隐藏,然后对其进行动画处理是不可行的选择。

【问题讨论】:

    标签: ios uikit splash-screen


    【解决方案1】:

    您需要的是第二个 UIWindowUIWindowLevelStatusBar 或更高的 windowLevel。您将在您的应用程序委托中创建两个UIWindow 对象,一个与您的常规视图层次结构,另一个与图像,并动画第二个淡出(或者你需要动画)。两个窗口都应该可见,并且启动窗口位于顶部。

    这种方法很复杂,因为您可能会遇到旋转问题,具体取决于您的常规视图层次结构。我们已经在我们的软件中做到了这一点,而且效果很好。


    编辑:

    自适应解决方案(窗口方法,非常简单):

    UIImageView* splashView = [[UIImageView alloc] initWithImage:[UIImage imageWithBaseName:@"Default"]];
    [splashView sizeToFit];
    
    UIViewController* tmpVC = [UIViewController new];
    [tmpVC.view setFrame:splashView.bounds];
    [tmpVC.view addSubview:splashView];
    
    // just by instantiating a UIWindow, it is automatically added to the app.
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    UIWindow *hudWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0f, -20.0f, keyWin.frame.size.width, keyWin.frame.size.height)];
    
    [hudWindow setBackgroundColor:[UIColor clearColor]];
    [hudWindow setRootViewController:tmpVC];
    [hudWindow setAlpha: 1.0];
    [hudWindow setWindowLevel:UIWindowLevelStatusBar+1];
    [hudWindow setHidden:NO];
    
    _hudWin = hudWindow;
    
    [UIView animateWithDuration:2.3f animations:^{
        [_hudWin setAlpha:0.f];
    } completion:^(BOOL finished) {
        [_hudWin removeFromSuperview];
        _hudWin = nil;
    }];
    

    最后,感谢 this guy


    更简单的方法是在隐藏状态栏的情况下启动您的应用程序,在视图层次结构的顶部放置您想要动画的视图,然后在动画完成后,使用[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide] 显示状态栏

    【讨论】:

    • 谢谢,我对UIWindow 方法的不理解是如何向应用程序添加新窗口?我不能[self.window addSubWindow];,如果你明白我的意思吗?
    • self.window 应该保留您的主要UIWindow。然后创建另一个UIWindow 对象,使其成为关键且可见,制作动画,最后使主窗口成为关键且可见。
    • 对不起,要么我没听懂你的意思,要么你的回答有误。经过搜索,我意识到只需创建一个UIWindow 实例,它就被添加到层次结构中,完全没有其他工作。剩下的就是按照你的建议把它移到前面。我将添加我找到的代码,并解决我的问题。谢谢。
    【解决方案2】:

    您可以在 App 的 UIWindow 中添加子视图。将 UIImageView 框架的 Y 坐标设置为 -20 像素,以便处理状态栏。

    将默认 PNG 图像添加到您的窗口并标记它:

    static const NSInteger kCSSplashScreenTag = 420; // pick any number!
    UIImageView *splashImageView;
    
    // Careful, this wont work for iPad!
    if ( [[UIScreen mainScreen] bounds].size.height > 480.0f ) // not best practice, but works for detecting iPhone5.
    {
        splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default-568h"]];
    }
    else
    {
        splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default"]];
    }
    
    splashImageView.frame = CGRectMake(0.0f, -20.0f, splashImageView.image.size.width, splashImageView.image.size.height);
    splashImageView.tag = kCSSplashScreenTag;
    [self.window addSubview:splashImageView];
    [splashImageView release];
    
    [self _fadeOutSplaceImageView];
    

    然后淡出

    - (void)_fadeOutSplashImageView
    {
        UIView *splashview = [self.window viewWithTag:kCSSplashScreenTag];
        if ( splashview != nil )
        {
            [UIView animateWithDuration:0.5
                                  delay:0.0
                                options:0
                             animations:^{
                                 splashview.alpha = 0.0f;
                             }
                             completion:^(BOOL finished) {
                                 [splashview removeFromSuperview];
                             }];
        }
    }
    

    【讨论】:

    • 如果我错了,请纠正我,我认为这段代码不会使 imageView 与状态栏重叠,例如,在状态栏的顶部。
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多