StartFigureViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // StartFigureViewController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/1/14.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "StartFigureViewController.h"
  9. @interface StartFigureViewController ()
  10. @property (nonatomic, readwrite, strong) UIImageView *launchImageView;
  11. @property (nonatomic, strong) AVPlayer *player;
  12. @end
  13. @implementation StartFigureViewController
  14. /// 重写init方法,配置你想要的属性
  15. - (instancetype)init
  16. {
  17. self = [super init];
  18. if (self) {
  19. }
  20. return self;
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // 隐藏导航条
  25. self.fd_prefersNavigationBarHidden = YES;
  26. /// 设置
  27. [self _setup];
  28. /// 设置导航栏
  29. [self _setupNavigationItem];
  30. /// 设置子控件
  31. [self _setupSubViews];
  32. /// 布局子空间
  33. [self _makeSubViewsConstraints];
  34. [self videoPlayCompleted];
  35. }
  36. - (void)viewDidDisappear:(BOOL)animated {
  37. [super viewDidDisappear:animated];
  38. // 删除监听
  39. [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
  40. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
  41. }
  42. #pragma mark — Override
  43. - (void)bindViewModel {
  44. }
  45. #pragma mark - 事件处理Or辅助方法
  46. // 获取启动图
  47. - (UIImage *)getLaunchImage {
  48. UIImage *launchImage = nil;
  49. NSString *viewOrientation = nil;
  50. CGSize viewSize = [UIScreen mainScreen].bounds.size;
  51. UIInterfaceOrientation *orientation = [[UIApplication sharedApplication] userInterfaceLayoutDirection];
  52. if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
  53. viewOrientation = @"Landscape";
  54. }else {
  55. viewOrientation = @"Portrait";
  56. }
  57. NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
  58. for (NSDictionary *dict in imagesDictionary) {
  59. CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
  60. if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
  61. launchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
  62. break;
  63. }
  64. }
  65. return launchImage;
  66. }
  67. - (void)videoPlayCompleted {
  68. if (self.videoPlaybackCompleted) {
  69. self.videoPlaybackCompleted();
  70. }
  71. }
  72. - (void)applicationDidBecomeActiveNotification {
  73. if (self.player) {
  74. [self.player play];
  75. }
  76. }
  77. #pragma mark - 初始化
  78. - (void)_setup{
  79. }
  80. #pragma mark - 设置导航栏
  81. - (void)_setupNavigationItem{
  82. }
  83. #pragma mark - 设置子控件
  84. - (void)_setupSubViews{
  85. // 显示封面
  86. UIImageView *launchImageView = [[UIImageView alloc] initWithFrame:SCREEN_BOUNDS];
  87. launchImageView.image = [self getLaunchImage];
  88. [self.view addSubview:launchImageView];
  89. return;
  90. // 播放视频
  91. NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"xy_app_start" ofType:@"mp4"]];
  92. AVPlayer *player = [AVPlayer playerWithURL:videoURL];
  93. AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  94. playerLayer.backgroundColor = Color_Clear.CGColor;
  95. playerLayer.frame = self.view.bounds;
  96. [self.view.layer addSublayer:playerLayer];
  97. [player play];
  98. self.player = player;
  99. // 添加监听
  100. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayCompleted) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
  101. // 在播放中进入后台会停止播放 再次进入前台需要继续播放
  102. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActiveNotification) name:UIApplicationDidBecomeActiveNotification object:nil];
  103. }
  104. #pragma mark - 布局子控件
  105. - (void)_makeSubViewsConstraints{
  106. }
  107. #pragma mark - Setter & Getter
  108. @end