一种启动动画的实现

版本记录

版本 时间
V1.0 2017.04.08

前言

很多时候我们的APP都有这种需求:启动的时候判断是否登录,如果没有登录就进入动画页和引导页,如果已经登录则直接进入APP的主页,一般主页都是UITabBarController嵌套UINavigationController。这里我主要就是做一个示例,说明下如何加载启动视频。

详细设计

设计思路:就是采用MPMoviePlayerController实现。

下面先看一下文档组织结构。

一种启动动画的实现_第1张图片
文档组织结构

下面就看代码了。

1. AppDelegate.m
#import "AppDelegate.h"
#import "JJTabBarVC.h"
#import "JJLoginVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    BOOL isLogin = NO;
    if (isLogin) {
        JJTabBarVC *tabBarVC = [[JJTabBarVC alloc] init];
        self.window.rootViewController = tabBarVC;
    }
    else {
        JJLoginVC *loginVC = [[JJLoginVC alloc] init];
        self.window.rootViewController = loginVC;
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}


- (void)applicationWillTerminate:(UIApplication *)application {
    
}

@end
2.  JJLoginVC.m
#import "JJLoginVC.h"
#import 

#define kJJLoginVCScreenWidth           ([UIScreen mainScreen].bounds.size.width)
#define kJJLoginVCScreenHeight          ([UIScreen mainScreen].bounds.size.height)


@interface JJLoginVC ()

@property (nonatomic, strong) MPMoviePlayerController *playerVC; //ios9.0废弃
@property (nonatomic, strong) UIButton *skipButton;


@end

@implementation JJLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor magentaColor];
    
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"mov"];
    NSURL *playURL = [NSURL fileURLWithPath:urlStr];
    self.playerVC = [[MPMoviePlayerController alloc] initWithContentURL:playURL];
    [self.view addSubview:self.playerVC.view];
    
    self.playerVC.controlStyle = MPMovieControlStyleNone;
    [self.playerVC.view setFrame:self.view.frame];
    self.playerVC.repeatMode = MPMovieRepeatModeOne;
    self.playerVC.shouldAutoplay = YES;
    self.playerVC.scalingMode = MPMovieScalingModeAspectFill;
    self.playerVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.playerVC play];
    
    UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    [skipButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    skipButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
    skipButton.alpha = 0.6;
    [self.view addSubview:skipButton];
    [skipButton sizeToFit];
    skipButton.frame = CGRectMake((kJJLoginVCScreenWidth - skipButton.bounds.size.width) * 0.5,
                                  kJJLoginVCScreenHeight - 50.0,
                                  skipButton.bounds.size.width,
                                  skipButton.bounds.size.height);
    [skipButton addTarget:self action:@selector(skipButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    self.skipButton = skipButton;
}

# pragma mark - Action/ Notification

- (void)skipButtonDidClick
{
    [self.playerVC stop];
    [self.playerVC.view removeFromSuperview];
    [self.skipButton removeFromSuperview];
}

@end

设计结果

  我们直接看下边的gif图。这里我是录制的一段变形金刚的宣传片作为素材的。

设计结果

后记

  继续谢谢大家的支持,up!有问题联系,这个周末还是在敲代码中度过的。

你可能感兴趣的:(一种启动动画的实现)