ZFLandScapeControlView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // ZFLandScapeControlView.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFLandScapeControlView.h"
  25. #import "UIView+ZFFrame.h"
  26. #import "ZFUtilities.h"
  27. #if __has_include(<ZFPlayer/ZFPlayer.h>)
  28. #import <ZFPlayer/ZFPlayerConst.h>
  29. #else
  30. #import "ZFPlayerConst.h"
  31. #endif
  32. @interface ZFLandScapeControlView () <ZFSliderViewDelegate>
  33. /// 顶部工具栏
  34. @property (nonatomic, strong) UIView *topToolView;
  35. /// 返回按钮
  36. @property (nonatomic, strong) UIButton *backBtn;
  37. /// 标题
  38. @property (nonatomic, strong) UILabel *titleLabel;
  39. /// 底部工具栏
  40. @property (nonatomic, strong) UIView *bottomToolView;
  41. /// 播放或暂停按钮
  42. @property (nonatomic, strong) UIButton *playOrPauseBtn;
  43. /// 播放的当前时间
  44. @property (nonatomic, strong) UILabel *currentTimeLabel;
  45. /// 滑杆
  46. @property (nonatomic, strong) ZFSliderView *slider;
  47. /// 视频总时间
  48. @property (nonatomic, strong) UILabel *totalTimeLabel;
  49. /// 锁定屏幕按钮
  50. @property (nonatomic, strong) UIButton *lockBtn;
  51. @property (nonatomic, assign) BOOL isShow;
  52. @end
  53. @implementation ZFLandScapeControlView
  54. - (void)dealloc {
  55. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
  56. }
  57. - (instancetype)initWithFrame:(CGRect)frame {
  58. if (self = [super initWithFrame:frame]) {
  59. [self addSubview:self.topToolView];
  60. [self.topToolView addSubview:self.backBtn];
  61. [self.topToolView addSubview:self.titleLabel];
  62. [self addSubview:self.bottomToolView];
  63. [self.bottomToolView addSubview:self.playOrPauseBtn];
  64. [self.bottomToolView addSubview:self.currentTimeLabel];
  65. [self.bottomToolView addSubview:self.slider];
  66. [self.bottomToolView addSubview:self.totalTimeLabel];
  67. [self addSubview:self.lockBtn];
  68. // 设置子控件的响应事件
  69. [self makeSubViewsAction];
  70. [self resetControlView];
  71. /// statusBarFrame changed
  72. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layoutControllerViews) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
  73. }
  74. return self;
  75. }
  76. - (void)layoutSubviews {
  77. [super layoutSubviews];
  78. CGFloat min_x = 0;
  79. CGFloat min_y = 0;
  80. CGFloat min_w = 0;
  81. CGFloat min_h = 0;
  82. CGFloat min_view_w = self.bounds.size.width;
  83. CGFloat min_view_h = self.bounds.size.height;
  84. CGFloat min_margin = 9;
  85. min_x = 0;
  86. min_y = 0;
  87. min_w = min_view_w;
  88. min_h = iPhoneX ? 110 : 80;
  89. self.topToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  90. min_x = (iPhoneX && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) ? 44: 15;
  91. if (@available(iOS 13.0, *)) {
  92. min_y = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ? 10 : (iPhoneX ? 40 : 20);
  93. } else {
  94. min_y = (iPhoneX && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) ? 10: (iPhoneX ? 40 : 20);
  95. }
  96. min_w = 40;
  97. min_h = 40;
  98. self.backBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  99. min_x = self.backBtn.zf_right + 5;
  100. min_y = 0;
  101. min_w = min_view_w - min_x - 15 ;
  102. min_h = 30;
  103. self.titleLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  104. self.titleLabel.zf_centerY = self.backBtn.zf_centerY;
  105. min_h = iPhoneX ? 100 : 73;
  106. min_x = 0;
  107. min_y = min_view_h - min_h;
  108. min_w = min_view_w;
  109. self.bottomToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  110. min_x = (iPhoneX && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) ? 44: 15;
  111. min_y = 32;
  112. min_w = 30;
  113. min_h = 30;
  114. self.playOrPauseBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  115. min_x = self.playOrPauseBtn.zf_right + 4;
  116. min_y = 0;
  117. min_w = 62;
  118. min_h = 30;
  119. self.currentTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  120. self.currentTimeLabel.zf_centerY = self.playOrPauseBtn.zf_centerY;
  121. min_w = 62;
  122. min_x = self.bottomToolView.zf_width - min_w - ((iPhoneX && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) ? 44: min_margin);
  123. min_y = 0;
  124. min_h = 30;
  125. self.totalTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  126. self.totalTimeLabel.zf_centerY = self.playOrPauseBtn.zf_centerY;
  127. min_x = self.currentTimeLabel.zf_right + 4;
  128. min_y = 0;
  129. min_w = self.totalTimeLabel.zf_left - min_x - 4;
  130. min_h = 30;
  131. self.slider.frame = CGRectMake(min_x, min_y, min_w, min_h);
  132. self.slider.zf_centerY = self.playOrPauseBtn.zf_centerY;
  133. min_x = (iPhoneX && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) ? 50: 18;
  134. min_y = 0;
  135. min_w = 40;
  136. min_h = 40;
  137. self.lockBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  138. self.lockBtn.zf_centerY = self.zf_centerY;
  139. if (!self.isShow) {
  140. self.topToolView.zf_y = -self.topToolView.zf_height;
  141. self.bottomToolView.zf_y = self.zf_height;
  142. self.lockBtn.zf_left = iPhoneX ? -82: -47;
  143. } else {
  144. self.lockBtn.zf_left = iPhoneX ? 50: 18;
  145. if (self.player.isLockedScreen) {
  146. self.topToolView.zf_y = -self.topToolView.zf_height;
  147. self.bottomToolView.zf_y = self.zf_height;
  148. } else {
  149. self.topToolView.zf_y = 0;
  150. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  151. }
  152. }
  153. }
  154. - (void)makeSubViewsAction {
  155. [self.backBtn addTarget:self action:@selector(backBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
  156. [self.playOrPauseBtn addTarget:self action:@selector(playPauseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  157. [self.lockBtn addTarget:self action:@selector(lockButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  158. }
  159. #pragma mark - action
  160. - (void)layoutControllerViews {
  161. [self layoutIfNeeded];
  162. [self setNeedsLayout];
  163. }
  164. - (void)backBtnClickAction:(UIButton *)sender {
  165. self.lockBtn.selected = NO;
  166. self.player.lockedScreen = NO;
  167. self.lockBtn.selected = NO;
  168. if (self.player.orientationObserver.supportInterfaceOrientation & ZFInterfaceOrientationMaskPortrait) {
  169. [self.player enterFullScreen:NO animated:YES];
  170. }
  171. if (self.backBtnClickCallback) {
  172. self.backBtnClickCallback();
  173. }
  174. }
  175. - (void)playPauseButtonClickAction:(UIButton *)sender {
  176. [self playOrPause];
  177. }
  178. /// 根据当前播放状态取反
  179. - (void)playOrPause {
  180. self.playOrPauseBtn.selected = !self.playOrPauseBtn.isSelected;
  181. self.playOrPauseBtn.isSelected? [self.player.currentPlayerManager play]: [self.player.currentPlayerManager pause];
  182. }
  183. - (void)playBtnSelectedState:(BOOL)selected {
  184. self.playOrPauseBtn.selected = selected;
  185. }
  186. - (void)lockButtonClickAction:(UIButton *)sender {
  187. sender.selected = !sender.selected;
  188. self.player.lockedScreen = sender.selected;
  189. }
  190. #pragma mark - ZFSliderViewDelegate
  191. - (void)sliderTouchBegan:(float)value {
  192. self.slider.isdragging = YES;
  193. }
  194. - (void)sliderTouchEnded:(float)value {
  195. if (self.player.totalTime > 0) {
  196. self.slider.isdragging = YES;
  197. if (self.sliderValueChanging) self.sliderValueChanging(value, self.slider.isForward);
  198. @weakify(self)
  199. [self.player seekToTime:self.player.totalTime*value completionHandler:^(BOOL finished) {
  200. @strongify(self)
  201. if (finished) {
  202. self.slider.isdragging = NO;
  203. if (self.sliderValueChanged) self.sliderValueChanged(value);
  204. if (self.seekToPlay) {
  205. [self.player.currentPlayerManager play];
  206. }
  207. }
  208. }];
  209. } else {
  210. self.slider.isdragging = NO;
  211. self.slider.value = 0;
  212. }
  213. }
  214. - (void)sliderValueChanged:(float)value {
  215. if (self.player.totalTime == 0) {
  216. self.slider.value = 0;
  217. return;
  218. }
  219. self.slider.isdragging = YES;
  220. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  221. self.currentTimeLabel.text = currentTimeString;
  222. if (self.sliderValueChanging) self.sliderValueChanging(value,self.slider.isForward);
  223. }
  224. - (void)sliderTapped:(float)value {
  225. [self sliderTouchEnded:value];
  226. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  227. self.currentTimeLabel.text = currentTimeString;
  228. }
  229. #pragma mark - public method
  230. /// 重置ControlView
  231. - (void)resetControlView {
  232. self.slider.value = 0;
  233. self.slider.bufferValue = 0;
  234. self.currentTimeLabel.text = @"00:00";
  235. self.totalTimeLabel.text = @"00:00";
  236. self.backgroundColor = [UIColor clearColor];
  237. self.playOrPauseBtn.selected = YES;
  238. self.titleLabel.text = @"";
  239. self.topToolView.alpha = 1;
  240. self.bottomToolView.alpha = 1;
  241. self.isShow = NO;
  242. }
  243. - (void)showControlView {
  244. self.lockBtn.alpha = 1;
  245. self.isShow = YES;
  246. if (self.player.isLockedScreen) {
  247. self.topToolView.zf_y = -self.topToolView.zf_height;
  248. self.bottomToolView.zf_y = self.zf_height;
  249. } else {
  250. self.topToolView.zf_y = 0;
  251. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  252. }
  253. self.lockBtn.zf_left = iPhoneX ? 50: 18;
  254. self.player.statusBarHidden = NO;
  255. if (self.player.isLockedScreen) {
  256. self.topToolView.alpha = 0;
  257. self.bottomToolView.alpha = 0;
  258. } else {
  259. self.topToolView.alpha = 1;
  260. self.bottomToolView.alpha = 1;
  261. }
  262. }
  263. - (void)hideControlView {
  264. self.isShow = NO;
  265. self.topToolView.zf_y = -self.topToolView.zf_height;
  266. self.bottomToolView.zf_y = self.zf_height;
  267. self.lockBtn.zf_left = iPhoneX ? -82: -47;
  268. self.player.statusBarHidden = YES;
  269. self.topToolView.alpha = 0;
  270. self.bottomToolView.alpha = 0;
  271. self.lockBtn.alpha = 0;
  272. }
  273. - (BOOL)shouldResponseGestureWithPoint:(CGPoint)point withGestureType:(ZFPlayerGestureType)type touch:(nonnull UITouch *)touch {
  274. CGRect sliderRect = [self.bottomToolView convertRect:self.slider.frame toView:self];
  275. if (CGRectContainsPoint(sliderRect, point)) {
  276. return NO;
  277. }
  278. if (self.player.isLockedScreen && type != ZFPlayerGestureTypeSingleTap) { // 锁定屏幕方向后只相应tap手势
  279. return NO;
  280. }
  281. return YES;
  282. }
  283. - (void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size {
  284. self.lockBtn.hidden = self.player.orientationObserver.fullScreenMode == ZFFullScreenModePortrait;
  285. }
  286. - (void)videoPlayer:(ZFPlayerController *)videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime {
  287. if (!self.slider.isdragging) {
  288. NSString *currentTimeString = [ZFUtilities convertTimeSecond:currentTime];
  289. self.currentTimeLabel.text = currentTimeString;
  290. NSString *totalTimeString = [ZFUtilities convertTimeSecond:totalTime];
  291. self.totalTimeLabel.text = totalTimeString;
  292. self.slider.value = videoPlayer.progress;
  293. }
  294. }
  295. - (void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime {
  296. self.slider.bufferValue = videoPlayer.bufferProgress;
  297. }
  298. - (void)showTitle:(NSString *)title fullScreenMode:(ZFFullScreenMode)fullScreenMode {
  299. self.titleLabel.text = title;
  300. self.player.orientationObserver.fullScreenMode = fullScreenMode;
  301. self.lockBtn.hidden = fullScreenMode == ZFFullScreenModePortrait;
  302. }
  303. /// 调节播放进度slider和当前时间更新
  304. - (void)sliderValueChanged:(CGFloat)value currentTimeString:(NSString *)timeString {
  305. self.slider.value = value;
  306. self.currentTimeLabel.text = timeString;
  307. self.slider.isdragging = YES;
  308. [UIView animateWithDuration:0.3 animations:^{
  309. self.slider.sliderBtn.transform = CGAffineTransformMakeScale(1.2, 1.2);
  310. }];
  311. }
  312. /// 滑杆结束滑动
  313. - (void)sliderChangeEnded {
  314. self.slider.isdragging = NO;
  315. [UIView animateWithDuration:0.3 animations:^{
  316. self.slider.sliderBtn.transform = CGAffineTransformIdentity;
  317. }];
  318. }
  319. #pragma mark - setter
  320. - (void)setFullScreenMode:(ZFFullScreenMode)fullScreenMode {
  321. _fullScreenMode = fullScreenMode;
  322. self.player.orientationObserver.fullScreenMode = fullScreenMode;
  323. self.lockBtn.hidden = fullScreenMode == ZFFullScreenModePortrait;
  324. }
  325. #pragma mark - getter
  326. - (UIView *)topToolView {
  327. if (!_topToolView) {
  328. _topToolView = [[UIView alloc] init];
  329. UIImage *image = ZFPlayer_Image(@"ZFPlayer_top_shadow");
  330. _topToolView.layer.contents = (id)image.CGImage;
  331. }
  332. return _topToolView;
  333. }
  334. - (UIButton *)backBtn {
  335. if (!_backBtn) {
  336. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  337. [_backBtn setImage:ZFPlayer_Image(@"ZFPlayer_back_full") forState:UIControlStateNormal];
  338. }
  339. return _backBtn;
  340. }
  341. - (UILabel *)titleLabel {
  342. if (!_titleLabel) {
  343. _titleLabel = [[UILabel alloc] init];
  344. _titleLabel.textColor = [UIColor whiteColor];
  345. _titleLabel.font = [UIFont systemFontOfSize:15.0];
  346. }
  347. return _titleLabel;
  348. }
  349. - (UIView *)bottomToolView {
  350. if (!_bottomToolView) {
  351. _bottomToolView = [[UIView alloc] init];
  352. UIImage *image = ZFPlayer_Image(@"ZFPlayer_bottom_shadow");
  353. _bottomToolView.layer.contents = (id)image.CGImage;
  354. }
  355. return _bottomToolView;
  356. }
  357. - (UIButton *)playOrPauseBtn {
  358. if (!_playOrPauseBtn) {
  359. _playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  360. [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_play") forState:UIControlStateNormal];
  361. [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_pause") forState:UIControlStateSelected];
  362. }
  363. return _playOrPauseBtn;
  364. }
  365. - (UILabel *)currentTimeLabel {
  366. if (!_currentTimeLabel) {
  367. _currentTimeLabel = [[UILabel alloc] init];
  368. _currentTimeLabel.textColor = [UIColor whiteColor];
  369. _currentTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  370. _currentTimeLabel.textAlignment = NSTextAlignmentCenter;
  371. }
  372. return _currentTimeLabel;
  373. }
  374. - (ZFSliderView *)slider {
  375. if (!_slider) {
  376. _slider = [[ZFSliderView alloc] init];
  377. _slider.delegate = self;
  378. _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
  379. _slider.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
  380. _slider.minimumTrackTintColor = [UIColor whiteColor];
  381. [_slider setThumbImage:ZFPlayer_Image(@"ZFPlayer_slider") forState:UIControlStateNormal];
  382. _slider.sliderHeight = 2;
  383. }
  384. return _slider;
  385. }
  386. - (UILabel *)totalTimeLabel {
  387. if (!_totalTimeLabel) {
  388. _totalTimeLabel = [[UILabel alloc] init];
  389. _totalTimeLabel.textColor = [UIColor whiteColor];
  390. _totalTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  391. _totalTimeLabel.textAlignment = NSTextAlignmentCenter;
  392. }
  393. return _totalTimeLabel;
  394. }
  395. - (UIButton *)lockBtn {
  396. if (!_lockBtn) {
  397. _lockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  398. [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_unlock-nor") forState:UIControlStateNormal];
  399. [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_lock-nor") forState:UIControlStateSelected];
  400. }
  401. return _lockBtn;
  402. }
  403. @end