Welcome

首页 / 移动开发 / IOS / IOS实现视频动画效果的启动图

先上效果图


实现思路
主要思路就是用一个控制器来作为播放视频的载体,然后在让这个控制器作为根视图,视频播放完成之后那就该干嘛干嘛了。
话不多说了,下面就放代码好了
先新建一个控制器AnimationViewController在控制器中新建一个属性moviePlayer,记得要先引入系统库<MediaPlayer/MediaPlayer.h>

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
设置moviePlayer我是在懒加载中直接设置的
-(MPMoviePlayerController *)moviePlayer{if (!_moviePlayer) {_moviePlayer = [[MPMoviePlayerController alloc]init];[_moviePlayer.view setFrame:self.view.bounds];//设置自动播放[_moviePlayer setShouldAutoplay:NO];//设置源类型 因为新特性一般都是播放本地的小视频 所以设置源类型为file_moviePlayer.movieSourceType = MPMovieSourceTypeFile;//取消控制视图 如:播放暂停等_moviePlayer.controlStyle = MPMovieControlStyleNone;[self.view addSubview:_moviePlayer.view];//监听播放完成[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playFinsihed) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];}return _moviePlayer;}
然后在.h中公开一个moviePath视频的路径,还有一个结束播放的blockplayFinished等下需要。
AnimationViewController中也算差不多了,毕竟也没什么东西,接下来我们去AppDelegate中声明一个AnimationViewController属性
- (AnimationViewController *)animationViewController{if (!_animationViewController) {_animationViewController = [[AnimationViewController alloc]init];//设置本地视频路径_animationViewController.moviePath = [[NSBundle mainBundle] pathForResource:@"V" ofType:@"mp4"];_animationViewController.playFinished = ^{UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];[UIApplication sharedApplication].keyWindow.rootViewController = rootNav;};}return _animationViewController;}
然后在AppDelegate的启动方法把这个控制器设为根视图
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.rootViewController = self.animationViewController;[self.window makeKeyAndVisible];return YES;}
总结
这里要说一句,刚开始我用这个路径但是一直为空,后来我添加了一个名字为Resource的文件夹把mp4放进去就好了,以上就是这篇文章的全部内容了,有需要的朋友们可以参考借鉴。