您的位置:首页 > 移动开发 > IOS开发

IOS实现视频动画效果的启动图

2016-09-01 15:08 921 查看

先上效果图

实现思路

主要思路就是用一个控制器来作为播放视频的载体,然后在让这个控制器作为根视图,视频播放完成之后那就该干嘛干嘛了。

话不多说了,下面就放代码好了

先新建一个控制器

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放进去就好了,以上就是这篇文章的全部内容了,有需要的朋友们可以参考借鉴。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 启动页 视频 动画