一行代碼實現(xiàn)iOS項目啟動頁, 包括加載網(wǎng)絡視頻, 本地視頻, 網(wǎng)絡圖片, 本地圖片
github下載地址:https://github.com/ZHHalsey/AppStart(感覺有用可以點個star)
可以設置倒計時
使用方法很簡單
導入寫好的類ZHMoviePlayerController, 創(chuàng)建一個對象, 然后根據(jù)項目需求調(diào)用下面的兩個對象方法(分加載視頻跟加載圖片,可以是網(wǎng)絡的也可以是本地的)
image
如果加載的時候, 如果只加載網(wǎng)絡圖片不加載本地圖片的話,本地圖片參數(shù)傳入為nil就可以,加載視頻同理
這里展示的demo沒加緩存, 我自己做的項目中加了緩存了,提供下思路在這里:
加緩存的話, 寫入沙盒, 設置一個userdefault, 啟動的時候先去沙盒找, 如果沙盒有的話就加載沙盒的, 沙盒沒有的話就加載網(wǎng)絡的, 然后把需要加載的啟動頁寫入沙盒就行
demo展示放不了視頻, 所以就放幾張圖片, 供大家參考
下面這個是視頻啟動頁,可以點擊按鈕進入應用, 也可以設置幾秒倒計時自動進入
image
下面這個是加載圖片啟動頁, 這里是用的倒計時的加載
image
貼上源碼:
#import "ViewController.h"
@interface ZHMoviePlayerController : ViewController
/**
* @param movieURL 網(wǎng)上url視頻
* @param localMovieName 本地視頻
*/
- (void)setMoviePlayerInIndexWithURL:(NSURL *)movieURL localMovieName:(NSString *)localMovieName;
/**
* @param imageURL 網(wǎng)上url圖片
* @param localImageName 本地圖片
* @param timeCount 倒計時時間
*/
- (void)setImageInIndexWithURL:(NSURL *)imageURL localImageName:(NSString *)localImageName timeCount:(int)timeCount;
@end
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#import "ZHMoviePlayerController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "ViewController.h"
@interface ZHMoviePlayerController ()
@property (nonatomic, strong)AVPlayerViewController *AVPlayer;
@property (nonatomic, strong)UIButton *enterMainButton;
@property (nonatomic, assign) int timeCount;
@property (nonatomic, weak)NSTimer *timer;
@property (nonatomic, weak)NSTimer *timer1;
@end
@implementation ZHMoviePlayerController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
}
- (void)setMoviePlayerInIndexWithURL:(NSURL *)movieURL localMovieName:(NSString *)localMovieName
{
self.AVPlayer = [[AVPlayerViewController alloc]init];
// 取消多分屏功能
self.AVPlayer.allowsPictureInPicturePlayback = NO;
self.AVPlayer.showsPlaybackControls = false;
AVPlayerItem *item;
if (movieURL) {
NSLog(@"傳入了網(wǎng)絡視頻url過來");
item = [[AVPlayerItem alloc]initWithURL:movieURL];
}else if (localMovieName) {
NSLog(@"加載的是本地的視頻");
NSString *path = [[NSBundle mainBundle] pathForResource:@"movie.mp4" ofType:nil];
NSLog(@"path---%@", path);
item = [[AVPlayerItem alloc]initWithURL:[NSURL fileURLWithPath:path]];
}
AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
// layer
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[layer setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
// 填充模式
// layer.videoGravity = AVLayerVideoGravityResizeAspect; // 保持視頻的縱橫比
layer.videoGravity = AVLayerVideoGravityResize; // 填充整個屏幕
self.AVPlayer.player = player;
[self.view.layer addSublayer:layer];
[self.AVPlayer.player play];
// 重復播放葛峻。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
// [self createLoginBtn]; // 3秒后自動就停止(這里自行選擇)
[self createLoginBtn1]; // 不點的話 就一直播放視頻
}
- (void)setImageInIndexWithURL:(NSURL *)imageURL localImageName:(NSString *)localImageName timeCount:(int)timeCount{
_timeCount = timeCount;
// http://fimg.yucuizhubao.com/img/start.png
UIImageView *imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
if (imageURL) {
NSLog(@"加載的是網(wǎng)絡上的圖片");
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image1 = [UIImage imageWithData:data];
imagev1.image = image1;
}
if (localImageName) {
NSLog(@"加載的是本地的圖片");
UIImage *image = [UIImage imageNamed:@"bj.png"];
imagev1.image = image;
}
[self.view addSubview:imagev1];
[self createLoginBtn];
}
// 播放完成代理
- (void)playDidEnd:(NSNotification *)Notification{
// 重新播放
[self.AVPlayer.player seekToTime:CMTimeMake(0, 1)];
[self.AVPlayer.player play];
}
// 用戶不用點擊, 幾秒后自動進入程序
- (void)createLoginBtn
{
// 進入按鈕
_enterMainButton = [[UIButton alloc] init];
_enterMainButton.frame = CGRectMake(SCREEN_WIDTH - 90, 50, 60, 30);
_enterMainButton.backgroundColor = [UIColor grayColor];
_enterMainButton.titleLabel.font = [UIFont systemFontOfSize:12];
_enterMainButton.layer.cornerRadius = 15;
NSString *title = [NSString stringWithFormat:@"跳過 %d", _timeCount];
[_enterMainButton setTitle:title forState:UIControlStateNormal];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DaoJiShi) userInfo:nil repeats:YES];
[self.view addSubview:_enterMainButton];
[_enterMainButton addTarget:self action:@selector(enterMainAction) forControlEvents:UIControlEventTouchUpInside];
}
// 倒計時
- (void)DaoJiShi{
if (_timeCount > 0) {
_timeCount -= 1;
NSString *title = [NSString stringWithFormat:@"跳過 %d", _timeCount];
[_enterMainButton setTitle:title forState:UIControlStateNormal];
}else{
[_timer invalidate];
_timer = nil;
[self enterMainAction];
}
}
// 不會自動停止, 需要用戶點擊按鈕才能進入應用
- (void)createLoginBtn1{ // 這里的時間是3秒后視頻頁面出現(xiàn)按鈕
_timer1 = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showClickBtn) userInfo:nil repeats:YES];
}
- (void)showClickBtn{
NSLog(@"顯示進入應用按鈕");
UIButton *btn = [[UIButton alloc] init];
btn.frame = CGRectMake(30, SCREEN_HEIGHT - 100, SCREEN_WIDTH - 60, 40);
btn.backgroundColor = [UIColor redColor];
btn.layer.cornerRadius = 20;
btn.alpha = 0.5;
[btn setTitle:@"進入應用" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(enterMainAction) forControlEvents:UIControlEventTouchUpInside];
[_timer1 invalidate];
_timer1 = nil;// timer置為nil
}
- (void)enterMainAction{
NSLog(@"點擊了進入應用按鈕");
ViewController *vc = [[ViewController alloc]init];
self.view.window.rootViewController = vc;
[self.AVPlayer.player pause];
}
@end
Appdelegate里的調(diào)用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 這是圖片還有視頻的url鏈接
NSString *getUrlStr = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; // 網(wǎng)絡視頻
// NSString *getUrlStr = @"http://fimg.yucuizhubao.com/img/start.png"; // 網(wǎng)絡圖片
NSLog(@"后綴是--%@", [getUrlStr substringFromIndex:[getUrlStr length] - 4]);
ZHMoviePlayerController *ZHVC = [[ZHMoviePlayerController alloc]init];
if ([[getUrlStr substringFromIndex:[getUrlStr length] - 4] isEqualToString:@".mp4"] ) {
NSLog(@"加載的是視頻");
// [ZHVC setMoviePlayerInIndexWithURL:[NSURL URLWithString:getUrlStr] localMovieName:nil]; // 加載網(wǎng)絡url視頻
[ZHVC setMoviePlayerInIndexWithURL:nil localMovieName:@"movie.mp4"]; // 加載本地視頻
self.window.rootViewController = ZHVC;
}else if ([[getUrlStr substringFromIndex:[getUrlStr length] - 4] isEqualToString:@".png"]){
NSLog(@"加載的是圖片");
// [ZHVC setImageInIndexWithURL:[NSURL URLWithString:getUrlStr] localImageName:nil timeCount:4];// 加載網(wǎng)絡圖片
[ZHVC setImageInIndexWithURL:nil localImageName:@"bj.png" timeCount:4]; // 加載本地圖片
self.window.rootViewController = ZHVC;
}
return YES;
}
作者:Haleszh
鏈接:http://www.reibang.com/p/240032c245ec
來源:簡書
著作權歸作者所有蛮穿。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權,非商業(yè)轉(zhuǎn)載請注明出處娇未。