每次应用变为活动状态时显示启动画面

时间:2015-05-05 17:18:59

标签: ios iphone splash-screen

我希望每次应用变为活动状态时都会显示启动画面。我创建了一个函数showSplash,我在applicationDidBecomeActive:

中调用了它
-(void)showSplash
{
    UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Default.png"]];
    [self.window.rootViewController.view addSubview: splashScreen];
    [self.window makeKeyAndVisible];
    NSLog(@"begin splash");
    [UIView animateWithDuration: 4.2
                          delay: 0.5
                        options: UIViewAnimationOptionCurveEaseOut
                     animations: ^{  
                         splashScreen.alpha = 0.0;
                     }
                     completion: ^ (BOOL finished) {
                         [splashScreen removeFromSuperview];
                         NSLog(@"end splash");
                     }
     ];
}  

这就是我调用此函数的方式:

- (void)applicationDidBecomeActive:(UIApplication *)application {
[self showSplash];
}

但没有出现闪屏。请纠正我。

14 个答案:

答案 0 :(得分:7)

如果您希望应用程序在每次返回时都有一个全新的开始,您还可以禁用后台执行,如Apple Documentation中所述(上一部分标题为"选择退出后台执行& #34):

  

如果您不希望自己的应用在后台运行,可以   通过添加明确选择退出背景   应用程序的 UIApplicationExitsOnSuspend 键(值为YES)    Info.plist 文件。

答案 1 :(得分:4)

添加启动视图后 - 将其置于前面

变化

UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Default.png"]];
[self.window.rootViewController.view addSubview: splashScreen];
[self.window makeKeyAndVisible];

UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Default.png"]];
[self.window addSubview: splashScreen];
[self.window bringSubviewToFront: splashScreen]; //!
[self.window makeKeyAndVisible];

答案 2 :(得分:2)

请确保您的窗口根视图控制器主视图是您想要显示启动时应用程序中的最顶层视图...

答案 3 :(得分:2)

检查splashScreen(UIImageView)的框架。将其框架设置为根视图控制器的边界。

答案 4 :(得分:2)

您可以尝试使用这样的递归搜索顶视图控制器:

- (UIViewController *)findTopViewController {
   return [self topViewControllerFrom:self.window.rootViewController];
}

- (UIViewController *)topViewControllerFrom:(UIViewController *)vc {
    if (vc.navigationController.visibleViewController != nil) {
        return [self topViewControllerFrom:vc.navigationController.visibleViewController];
    }
    if (vc.tabBarController.selectedViewController != nil) {
        return [self topViewControllerFrom:vc.tabBarController.selectedViewController];
    }
    return vc;
}

现在调用[self findTopViewController]应该会返回您应用的当前可见/顶级VC,您可以这样做:

[[self findTopViewController].view addSubview:splashScreen];
...

答案 5 :(得分:2)

你可以这样做:

#import "AppDelegate.h"

#define kSplashScreen (UIScreen.mainScreen.bounds.size.height == 568) ? @"Default-568h" \
            : (UIScreen.mainScreen.bounds.size.height == 667) ? @"Default-667" \
            : (UIScreen.mainScreen.bounds.size.height == 736) ? @"Default-Portrait" \
            : @"Default"

@interface AppDelegate ()
@property (nonatomic) UIWindow *splashWindow;
@property (nonatomic) UIWindow *keyWindow;
@property (nonatomic, getter=isSplashConfigured) BOOL splashConfigured;
@property (nonatomic, getter=isShowingSplash) BOOL showingSplash;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self animateSplash];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self showOrHideSplash];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self showOrHideSplash];
}

- (void)showOrHideSplash
{
    [self.splashWindow setHidden:[self isShowingSplash]];
    if ([self isShowingSplash])
    {
        [self.keyWindow makeKeyAndVisible];
    }
    else
    {
        [self.splashWindow makeKeyAndVisible];
        [self animateSplash];
    }
    self.showingSplash = !self.showingSplash;
}


- (void)animateSplash
{
    if ([self isSplashConfigured])
    {
        [self.window.rootViewController.view addSubview:self.splashWindow];
        [self.window makeKeyAndVisible];
    }
    else
    {
        self.keyWindow = [[UIApplication sharedApplication] keyWindow];
        self.splashWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.splashWindow.rootViewController = [[UIViewController alloc] init];
        self.splashWindow.rootViewController.view.frame = self.splashWindow.bounds;
        UIImageView *splashImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kSplashScreen]];
        splashImage.frame = self.splashWindow.bounds;
        [self.splashWindow.rootViewController.view addSubview:splashImage];
        [self.splashWindow makeKeyAndVisible];
        [self.splashWindow setHidden:YES];
        self.splashConfigured = YES;
    }
    NSLog(@"begin splash");
    __weak AppDelegate* weakSelf = self;
    [UIView animateWithDuration:4.2 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations: ^{
        weakSelf.splashWindow.alpha = 1.0;
        weakSelf.splashWindow.alpha = 0.0;
    } completion: ^(BOOL finished) {
        [weakSelf.splashWindow removeFromSuperview];
        NSLog(@"end splash");
    }];
}
@end

答案 6 :(得分:2)

这是一个有点麻烦的解决方案,但它工作正常。这个想法是在所有控制器之上添加新的UIWindow

- (void)showSplash {
    id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[appDelegate window].frame];
    UIViewController *splashController = [UIViewController new];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
    splashController.view = imgView;
    window.rootViewController = splashController;
    window.windowLevel = UIWindowLevelAlert;
    window.hidden = NO;
    [window makeKeyAndVisible];
    [UIView animateWithDuration:4.2 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        window.alpha = 0.0;
    } completion:^(BOOL finished) {
        window.hidden = YES;
        window.rootViewController = nil;
        [[appDelegate window] makeKeyAndVisible];
        [[appDelegate window] setNeedsDisplay];
    }];
}

答案 7 :(得分:2)

请在下面找到解决此问题的方法。

将启动画面代码拆分为两种方法; showSplashhideSplash

showSplash方法中调用applicationWillResignActive。在该方法中,创建imageView并将其添加到rootViewController;将它的alpha设置为0.0f,然后将其设置为alpha 1.0f。这是为了确保用户在应用程序进入后台时看不到imageView。

现在拨打hideSplash中的applicationDidBecomeActive。在hideSplash中删除带有动画的imageView。请参阅以下代码;

-(void)showSplash
{
    _splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Default"]];
    [self.window.rootViewController.view addSubview:_splashScreen];
    _splashScreen.alpha = 0.0f;
    [UIView animateWithDuration:0.3f animations:^{
        _splashScreen.alpha = 1.0f;
    }];
}
- (void)hideSplash
{
    [UIView animateWithDuration: 4.2
                          delay: 0.5
                        options: UIViewAnimationOptionCurveEaseOut
                     animations: ^{
                         _splashScreen.alpha = 0.0;
                     }
                     completion: ^ (BOOL finished) {
                         [_splashScreen removeFromSuperview];
                         NSLog(@"end splash");
                     }
     ];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    [self showSplash];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self hideSplash];
}

我希望这有帮助! : - )

答案 8 :(得分:2)

添加

splashScreen.frame = self.window.rootViewController.view.frame;

以下

UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"alertBg.png"]];

例如

-(void)showSplash
{
    UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Default.png"]];
    splashScreen.frame = self.window.rootViewController.view.frame;
    [self.window.rootViewController.view addSubview: splashScreen];
    [self.window makeKeyAndVisible];
    NSLog(@"begin splash");
    [UIView animateWithDuration: 4.2
                          delay: 0.5
                        options: UIViewAnimationOptionCurveEaseOut
                     animations: ^{
                         splashScreen.alpha = 0.0;
                     }
                     completion: ^ (BOOL finished) {
                         [splashScreen removeFromSuperview];
                         NSLog(@"end splash");
                     }
     ];
}

答案 9 :(得分:2)

实现此目的的最佳方法是在AppDelegate's window中添加图片。 在下面的代码中,statusView是视图之一,由一些图像组成。因此,将其作为AppDelegate's window

添加到subView
[[[[UIApplication sharedApplication] delegate] window] addSubview:statusView];

现在,只要您希望它显示一段时间,您就会显示此视图,但同时将其显示在前面

-(void)showStatusView{

    [UIView animateWithDuration:0.5 animations:^{
        [[[[UIApplication sharedApplication] delegate] window] bringSubviewToFront:statusView];
        statusView.alpha = 1;
    }];
}

最好在AppDelegate's方法didBecomeActive上调用上述方法。

此外,只要应用程序重新启动活动,就会显示启动画面。这样,iOS将拍摄屏幕快照,当应用程序即将变为活动状态时,屏幕快照将显示几秒钟。

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self showStatusView];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self showStatusView];
}

应用处于有效状态后,您可以在一段时间内显示实际的启动画面。那么你平常的app状态。

答案 10 :(得分:2)

首先在didFinishLaunchingWithOptions函数中创建启动屏幕或图像。

AppDelegate.m

在此之后写下你的代码 -

enter image description here

答案 11 :(得分:2)

根视图控制器并不总是呈现的视图控制器。这可能会导致您的启动画面隐藏在一堆其他视图控制器下。尝试使用类似(swift syntex)的东西:

func getTopViewController() -> UIViewController?{
    if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController{
        while ((topController.presentedViewController) != nil) {
            topController = topController.presentedViewController!
        }
        return topController
    }
    return nil
}

将您的初始视图添加到顶视图控制器

答案 12 :(得分:1)

每次都正常启动应用程序,在applicationBecomeActive中调用您的方法,并在Appdelegate.window上添加图像视图,并在一段时间后通过计时器将其删除。

[self.window addSubview: splashScreen];

答案 13 :(得分:0)

您应该使用以下行:

[self.window addSubview: splashScreen];

而不是

[self.window.rootViewController.view addSubview: splashScreen];