ZXVideoPlayerControlView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // ZXVideoPlayerControlView.m
  3. // ZXVideoPlayer
  4. //
  5. // Created by Shawn on 16/4/21.
  6. // Copyright © 2016年 Shawn. All rights reserved.
  7. //
  8. #import "ZXVideoPlayerControlView.h"
  9. static const CGFloat kVideoControlBarHeight = 20.0 + 30.0;
  10. static const CGFloat kVideoControlAnimationTimeInterval = 0.3;
  11. static const CGFloat kVideoControlTimeLabelFontSize = 10.0;
  12. static const CGFloat kVideoControlBarAutoFadeOutTimeInterval = 5.0;
  13. @interface ZXVideoPlayerControlView ()
  14. @property (nonatomic, strong) UIView *topBar;
  15. @property (nonatomic, strong) UIView *bottomBar;
  16. @property (nonatomic, strong) UIButton *playButton;
  17. @property (nonatomic, strong) UIButton *pauseButton;
  18. @property (nonatomic, strong) UIButton *fullScreenButton;
  19. @property (nonatomic, strong) UIButton *shrinkScreenButton;
  20. @property (nonatomic, strong) UISlider *progressSlider;
  21. @property (nonatomic, strong) UILabel *timeLabel;
  22. @property (nonatomic, assign) BOOL isBarShowing;
  23. @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
  24. @end
  25. @implementation ZXVideoPlayerControlView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. self.backgroundColor = [UIColor clearColor];
  31. [self addSubview:self.topBar];
  32. [self addSubview:self.bottomBar];
  33. [self.bottomBar addSubview:self.playButton];
  34. [self.bottomBar addSubview:self.pauseButton];
  35. self.pauseButton.hidden = YES;
  36. [self.bottomBar addSubview:self.fullScreenButton];
  37. [self.bottomBar addSubview:self.shrinkScreenButton];
  38. self.shrinkScreenButton.hidden = YES;
  39. [self.bottomBar addSubview:self.progressSlider];
  40. [self.bottomBar addSubview:self.timeLabel];
  41. [self addSubview:self.indicatorView];
  42. // 返回按钮
  43. [self.topBar addSubview:self.backButton];
  44. // 锁定按钮
  45. [self.topBar addSubview:self.lockButton];
  46. // 缓冲进度条
  47. [self.bottomBar insertSubview:self.bufferProgressView belowSubview:self.progressSlider];
  48. // 快进、快退指示器
  49. [self addSubview:self.timeIndicatorView];
  50. // 亮度指示器
  51. [self addSubview:self.brightnessIndicatorView];
  52. // 音量指示器
  53. [self addSubview:self.volumeIndicatorView];
  54. // 电池条
  55. // [self.topBar addSubview:self.batteryView];
  56. // 标题
  57. [self.topBar addSubview:self.titleLabel];
  58. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
  59. [self addGestureRecognizer:tapGesture];
  60. }
  61. return self;
  62. }
  63. - (void)layoutSubviews
  64. {
  65. [super layoutSubviews];
  66. self.topBar.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), CGRectGetWidth(self.bounds), kVideoControlBarHeight);
  67. self.bottomBar.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetHeight(self.bounds) - kVideoControlBarHeight, CGRectGetWidth(self.bounds), kVideoControlBarHeight);
  68. self.playButton.frame = CGRectMake(CGRectGetMinX(self.bottomBar.bounds), CGRectGetHeight(self.bottomBar.bounds)/2 - CGRectGetHeight(self.playButton.bounds)/2, CGRectGetWidth(self.playButton.bounds), CGRectGetHeight(self.playButton.bounds));
  69. self.pauseButton.frame = self.playButton.frame;
  70. self.fullScreenButton.frame = CGRectMake(CGRectGetWidth(self.bottomBar.bounds) - CGRectGetWidth(self.fullScreenButton.bounds), CGRectGetHeight(self.bottomBar.bounds)/2 - CGRectGetHeight(self.fullScreenButton.bounds)/2, CGRectGetWidth(self.fullScreenButton.bounds), CGRectGetHeight(self.fullScreenButton.bounds));
  71. self.shrinkScreenButton.frame = self.fullScreenButton.frame;
  72. self.progressSlider.frame = CGRectMake(CGRectGetMaxX(self.playButton.frame), 0, CGRectGetMinX(self.fullScreenButton.frame) - CGRectGetMaxX(self.playButton.frame), kVideoControlBarHeight);
  73. self.timeLabel.frame = CGRectMake(CGRectGetMidX(self.progressSlider.frame), CGRectGetHeight(self.bottomBar.bounds) - CGRectGetHeight(self.timeLabel.bounds) - 2.0, CGRectGetWidth(self.progressSlider.bounds)/2, CGRectGetHeight(self.timeLabel.bounds));
  74. self.indicatorView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  75. // 返回按钮
  76. self.backButton.frame = CGRectMake(CGRectGetMinX(self.topBar.bounds), CGRectGetHeight(self.topBar.bounds) - 40, 40, 40);
  77. // 锁定按钮
  78. self.lockButton.frame = CGRectMake(CGRectGetMaxX(self.topBar.bounds) - 40 - 10, CGRectGetHeight(self.topBar.bounds) - 40, 40, 40);
  79. // 缓冲进度条
  80. self.bufferProgressView.bounds = CGRectMake(0, 0, self.progressSlider.bounds.size.width - 7, self.progressSlider.bounds.size.height);
  81. self.bufferProgressView.center = CGPointMake(self.progressSlider.center.x + 2, self.progressSlider.center.y);
  82. // 快进、快退指示器
  83. self.timeIndicatorView.center = self.indicatorView.center;
  84. // 亮度指示器
  85. self.brightnessIndicatorView.center = self.indicatorView.center;
  86. // 音量指示器
  87. self.volumeIndicatorView.center = self.indicatorView.center;
  88. // 电池条
  89. // self.batteryView.frame = CGRectMake(CGRectGetMinX(self.lockButton.frame) - CGRectGetWidth(self.batteryView.bounds) - 10, CGRectGetMidY(self.topBar.bounds) - CGRectGetHeight(self.batteryView.bounds) / 2, CGRectGetWidth(self.batteryView.bounds), CGRectGetHeight(self.batteryView.bounds));
  90. // 标题
  91. self.titleLabel.frame = CGRectMake(CGRectGetWidth(self.backButton.bounds), 20, CGRectGetWidth(self.topBar.bounds) - CGRectGetWidth(self.backButton.bounds) - CGRectGetWidth(self.lockButton.bounds), kVideoControlBarHeight - 20);
  92. }
  93. - (void)didMoveToSuperview
  94. {
  95. [super didMoveToSuperview];
  96. self.isBarShowing = YES;
  97. }
  98. #pragma mark - Public Method
  99. - (void)animateHide
  100. {
  101. if (!self.isBarShowing) {
  102. return;
  103. }
  104. [[NSNotificationCenter defaultCenter] postNotificationName:kZXPlayerControlViewHideNotification object:nil];
  105. [UIView animateWithDuration:kVideoControlAnimationTimeInterval animations:^{
  106. self.topBar.alpha = 0.0;
  107. self.bottomBar.alpha = 0.0;
  108. } completion:^(BOOL finished) {
  109. self.isBarShowing = NO;
  110. }];
  111. }
  112. - (void)animateShow
  113. {
  114. if (self.isBarShowing) {
  115. return;
  116. }
  117. [UIView animateWithDuration:kVideoControlAnimationTimeInterval animations:^{
  118. self.topBar.alpha = 1.0;
  119. self.bottomBar.alpha = 1.0;
  120. } completion:^(BOOL finished) {
  121. self.isBarShowing = YES;
  122. [self autoFadeOutControlBar];
  123. }];
  124. }
  125. - (void)autoFadeOutControlBar
  126. {
  127. if (!self.isBarShowing) {
  128. return;
  129. }
  130. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
  131. [self performSelector:@selector(animateHide) withObject:nil afterDelay:kVideoControlBarAutoFadeOutTimeInterval];
  132. }
  133. - (void)cancelAutoFadeOutControlBar
  134. {
  135. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
  136. }
  137. #pragma mark - Tap Detection
  138. - (void)onTap:(UITapGestureRecognizer *)gesture
  139. {
  140. if (gesture.state == UIGestureRecognizerStateRecognized) {
  141. if (self.isBarShowing) {
  142. [self animateHide];
  143. } else {
  144. [self animateShow];
  145. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  146. }
  147. }
  148. }
  149. #pragma mark - getters
  150. - (UIView *)topBar
  151. {
  152. if (!_topBar) {
  153. _topBar = [UIView new];
  154. _topBar.accessibilityIdentifier = @"TopBar";
  155. _topBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
  156. }
  157. return _topBar;
  158. }
  159. - (UIView *)bottomBar
  160. {
  161. if (!_bottomBar) {
  162. _bottomBar = [UIView new];
  163. _bottomBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
  164. }
  165. return _bottomBar;
  166. }
  167. - (UIButton *)playButton
  168. {
  169. if (!_playButton) {
  170. _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  171. [_playButton setImage:[UIImage imageNamed:@"kr-video-player-play"] forState:UIControlStateNormal];
  172. _playButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
  173. }
  174. return _playButton;
  175. }
  176. - (UIButton *)pauseButton
  177. {
  178. if (!_pauseButton) {
  179. _pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  180. [_pauseButton setImage:[UIImage imageNamed:@"kr-video-player-pause"] forState:UIControlStateNormal];
  181. _pauseButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
  182. }
  183. return _pauseButton;
  184. }
  185. - (UIButton *)fullScreenButton
  186. {
  187. if (!_fullScreenButton) {
  188. _fullScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  189. [_fullScreenButton setImage:[UIImage imageNamed:@"kr-video-player-fullscreen"] forState:UIControlStateNormal];
  190. _fullScreenButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
  191. }
  192. return _fullScreenButton;
  193. }
  194. - (UIButton *)shrinkScreenButton
  195. {
  196. if (!_shrinkScreenButton) {
  197. _shrinkScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  198. [_shrinkScreenButton setImage:[UIImage imageNamed:@"kr-video-player-shrinkscreen"] forState:UIControlStateNormal];
  199. _shrinkScreenButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
  200. }
  201. return _shrinkScreenButton;
  202. }
  203. - (UISlider *)progressSlider
  204. {
  205. if (!_progressSlider) {
  206. _progressSlider = [[UISlider alloc] init];
  207. [_progressSlider setThumbImage:[UIImage imageNamed:@"kr-video-player-point"] forState:UIControlStateNormal];
  208. [_progressSlider setMinimumTrackTintColor:[UIColor whiteColor]];
  209. [_progressSlider setMaximumTrackTintColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.4]];
  210. _progressSlider.value = 0.f;
  211. _progressSlider.continuous = YES;
  212. }
  213. return _progressSlider;
  214. }
  215. - (UILabel *)timeLabel
  216. {
  217. if (!_timeLabel) {
  218. _timeLabel = [UILabel new];
  219. _timeLabel.backgroundColor = [UIColor clearColor];
  220. _timeLabel.font = [UIFont systemFontOfSize:kVideoControlTimeLabelFontSize];
  221. _timeLabel.textColor = [UIColor whiteColor];
  222. _timeLabel.textAlignment = NSTextAlignmentRight;
  223. _timeLabel.bounds = CGRectMake(0, 0, kVideoControlTimeLabelFontSize, kVideoControlTimeLabelFontSize);
  224. }
  225. return _timeLabel;
  226. }
  227. - (UIActivityIndicatorView *)indicatorView
  228. {
  229. if (!_indicatorView) {
  230. _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  231. [_indicatorView stopAnimating];
  232. }
  233. return _indicatorView;
  234. }
  235. - (UIButton *)backButton
  236. {
  237. if (!_backButton) {
  238. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kVideoControlBarHeight , kVideoControlBarHeight)];
  239. [_backButton setImage:[UIImage imageNamed:@"zx-video-banner-back"] forState:UIControlStateNormal];
  240. _backButton.contentEdgeInsets = UIEdgeInsetsMake(5, 0, -5, 0);
  241. _backButton.imageEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6);
  242. }
  243. return _backButton;
  244. }
  245. - (UIButton *)lockButton
  246. {
  247. if (!_lockButton) {
  248. _lockButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight)];
  249. [_lockButton setImage:[UIImage imageNamed:@"zx-video-player-unlock"] forState:UIControlStateNormal];
  250. [_lockButton setImage:[UIImage imageNamed:@"zx-video-player-lock"] forState:UIControlStateHighlighted];
  251. [_lockButton setImage:[UIImage imageNamed:@"zx-video-player-lock"] forState:UIControlStateSelected];
  252. _lockButton.contentEdgeInsets = UIEdgeInsetsMake(5, 0, -5, 0);
  253. }
  254. return _lockButton;
  255. }
  256. - (UIProgressView *)bufferProgressView
  257. {
  258. if (!_bufferProgressView) {
  259. _bufferProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  260. _bufferProgressView.progressTintColor = [UIColor colorWithWhite:1 alpha:0.3];
  261. _bufferProgressView.trackTintColor = [UIColor clearColor];
  262. }
  263. return _bufferProgressView;
  264. }
  265. - (ZXVideoPlayerTimeIndicatorView *)timeIndicatorView
  266. {
  267. if (!_timeIndicatorView) {
  268. _timeIndicatorView = [[ZXVideoPlayerTimeIndicatorView alloc] initWithFrame:CGRectMake(0, 0, kVideoTimeIndicatorViewSide, kVideoTimeIndicatorViewSide)];
  269. }
  270. return _timeIndicatorView;
  271. }
  272. - (ZXVideoPlayerBrightnessView *)brightnessIndicatorView
  273. {
  274. if (!_brightnessIndicatorView) {
  275. _brightnessIndicatorView = [[ZXVideoPlayerBrightnessView alloc] initWithFrame:CGRectMake(0, 0, kVideoBrightnessIndicatorViewSide, kVideoBrightnessIndicatorViewSide)];
  276. }
  277. return _brightnessIndicatorView;
  278. }
  279. - (ZXVideoPlayerVolumeView *)volumeIndicatorView
  280. {
  281. if (!_volumeIndicatorView) {
  282. _volumeIndicatorView = [[ZXVideoPlayerVolumeView alloc] initWithFrame:CGRectMake(0, 0, kVideoVolumeIndicatorViewSide, kVideoVolumeIndicatorViewSide)];
  283. }
  284. return _volumeIndicatorView;
  285. }
  286. - (ZXVideoPlayerBatteryView *)batteryView
  287. {
  288. if (!_batteryView) {
  289. _batteryView = [[ZXVideoPlayerBatteryView alloc] initWithFrame:CGRectMake(0, 0, kVideoBatteryViewWidth, kVideoBatteryViewHeight)];
  290. }
  291. return _batteryView;
  292. }
  293. - (UILabel *)titleLabel
  294. {
  295. if (!_titleLabel) {
  296. _titleLabel = [[UILabel alloc] init];
  297. _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  298. _titleLabel.textColor = [UIColor whiteColor];
  299. _titleLabel.textAlignment = NSTextAlignmentCenter;
  300. }
  301. return _titleLabel;
  302. }
  303. @end