ZFLandScapeControlView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/ZFPlayer.h>
  29. #else
  30. #import "ZFPlayer.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. } else {
  143. if (self.player.isLockedScreen) {
  144. self.topToolView.zf_y = -self.topToolView.zf_height;
  145. self.bottomToolView.zf_y = self.zf_height;
  146. } else {
  147. self.topToolView.zf_y = 0;
  148. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  149. }
  150. }
  151. }
  152. - (void)makeSubViewsAction {
  153. [self.backBtn addTarget:self action:@selector(backBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
  154. [self.playOrPauseBtn addTarget:self action:@selector(playPauseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  155. [self.lockBtn addTarget:self action:@selector(lockButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  156. }
  157. #pragma mark - action
  158. - (void)layoutControllerViews {
  159. [self layoutIfNeeded];
  160. [self setNeedsLayout];
  161. }
  162. - (void)backBtnClickAction:(UIButton *)sender {
  163. self.lockBtn.selected = NO;
  164. self.player.lockedScreen = NO;
  165. self.lockBtn.selected = NO;
  166. if (self.player.orientationObserver.supportInterfaceOrientation & ZFInterfaceOrientationMaskPortrait) {
  167. [self.player enterFullScreen:NO animated:YES];
  168. }
  169. if (self.backBtnClickCallback) {
  170. self.backBtnClickCallback();
  171. }
  172. }
  173. - (void)playPauseButtonClickAction:(UIButton *)sender {
  174. [self playOrPause];
  175. }
  176. /// 根据当前播放状态取反
  177. - (void)playOrPause {
  178. self.playOrPauseBtn.selected = !self.playOrPauseBtn.isSelected;
  179. self.playOrPauseBtn.isSelected? [self.player.currentPlayerManager play]: [self.player.currentPlayerManager pause];
  180. }
  181. - (void)playBtnSelectedState:(BOOL)selected {
  182. self.playOrPauseBtn.selected = selected;
  183. }
  184. - (void)lockButtonClickAction:(UIButton *)sender {
  185. sender.selected = !sender.selected;
  186. self.player.lockedScreen = sender.selected;
  187. }
  188. #pragma mark - ZFSliderViewDelegate
  189. - (void)sliderTouchBegan:(float)value {
  190. self.slider.isdragging = YES;
  191. }
  192. - (void)sliderTouchEnded:(float)value {
  193. if (self.player.totalTime > 0) {
  194. self.slider.isdragging = YES;
  195. if (self.sliderValueChanging) self.sliderValueChanging(value, self.slider.isForward);
  196. @weakify(self)
  197. [self.player seekToTime:self.player.totalTime*value completionHandler:^(BOOL finished) {
  198. @strongify(self)
  199. if (finished) {
  200. self.slider.isdragging = NO;
  201. if (self.sliderValueChanged) self.sliderValueChanged(value);
  202. if (self.seekToPlay) {
  203. [self.player.currentPlayerManager play];
  204. }
  205. }
  206. }];
  207. } else {
  208. self.slider.isdragging = NO;
  209. self.slider.value = 0;
  210. }
  211. }
  212. - (void)sliderValueChanged:(float)value {
  213. if (self.player.totalTime == 0) {
  214. self.slider.value = 0;
  215. return;
  216. }
  217. self.slider.isdragging = YES;
  218. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  219. self.currentTimeLabel.text = currentTimeString;
  220. if (self.sliderValueChanging) self.sliderValueChanging(value,self.slider.isForward);
  221. }
  222. - (void)sliderTapped:(float)value {
  223. [self sliderTouchEnded:value];
  224. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  225. self.currentTimeLabel.text = currentTimeString;
  226. }
  227. #pragma mark - public method
  228. /// 重置ControlView
  229. - (void)resetControlView {
  230. self.slider.value = 0;
  231. self.slider.bufferValue = 0;
  232. self.currentTimeLabel.text = @"00:00";
  233. self.totalTimeLabel.text = @"00:00";
  234. self.backgroundColor = [UIColor clearColor];
  235. self.playOrPauseBtn.selected = YES;
  236. self.titleLabel.text = @"";
  237. self.topToolView.alpha = 1;
  238. self.bottomToolView.alpha = 1;
  239. self.isShow = NO;
  240. }
  241. - (void)showControlView {
  242. self.lockBtn.alpha = 1;
  243. self.isShow = YES;
  244. if (self.player.isLockedScreen) {
  245. self.topToolView.zf_y = -self.topToolView.zf_height;
  246. self.bottomToolView.zf_y = self.zf_height;
  247. } else {
  248. self.topToolView.zf_y = 0;
  249. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  250. }
  251. self.lockBtn.zf_left = iPhoneX ? 50: 18;
  252. self.player.statusBarHidden = NO;
  253. if (self.player.isLockedScreen) {
  254. self.topToolView.alpha = 0;
  255. self.bottomToolView.alpha = 0;
  256. } else {
  257. self.topToolView.alpha = 1;
  258. self.bottomToolView.alpha = 1;
  259. }
  260. }
  261. - (void)hideControlView {
  262. self.isShow = NO;
  263. self.topToolView.zf_y = -self.topToolView.zf_height;
  264. self.bottomToolView.zf_y = self.zf_height;
  265. self.lockBtn.zf_left = iPhoneX ? -82: -47;
  266. self.player.statusBarHidden = YES;
  267. self.topToolView.alpha = 0;
  268. self.bottomToolView.alpha = 0;
  269. self.lockBtn.alpha = 0;
  270. }
  271. - (BOOL)shouldResponseGestureWithPoint:(CGPoint)point withGestureType:(ZFPlayerGestureType)type touch:(nonnull UITouch *)touch {
  272. CGRect sliderRect = [self.bottomToolView convertRect:self.slider.frame toView:self];
  273. if (CGRectContainsPoint(sliderRect, point)) {
  274. return NO;
  275. }
  276. if (self.player.isLockedScreen && type != ZFPlayerGestureTypeSingleTap) { // 锁定屏幕方向后只相应tap手势
  277. return NO;
  278. }
  279. return YES;
  280. }
  281. - (void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size {
  282. self.lockBtn.hidden = self.player.orientationObserver.fullScreenMode == ZFFullScreenModePortrait;
  283. }
  284. - (void)videoPlayer:(ZFPlayerController *)videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime {
  285. if (!self.slider.isdragging) {
  286. NSString *currentTimeString = [ZFUtilities convertTimeSecond:currentTime];
  287. self.currentTimeLabel.text = currentTimeString;
  288. NSString *totalTimeString = [ZFUtilities convertTimeSecond:totalTime];
  289. self.totalTimeLabel.text = totalTimeString;
  290. self.slider.value = videoPlayer.progress;
  291. }
  292. }
  293. - (void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime {
  294. self.slider.bufferValue = videoPlayer.bufferProgress;
  295. }
  296. - (void)showTitle:(NSString *)title fullScreenMode:(ZFFullScreenMode)fullScreenMode {
  297. self.titleLabel.text = title;
  298. self.player.orientationObserver.fullScreenMode = fullScreenMode;
  299. self.lockBtn.hidden = fullScreenMode == ZFFullScreenModePortrait;
  300. }
  301. /// 调节播放进度slider和当前时间更新
  302. - (void)sliderValueChanged:(CGFloat)value currentTimeString:(NSString *)timeString {
  303. self.slider.value = value;
  304. self.currentTimeLabel.text = timeString;
  305. self.slider.isdragging = YES;
  306. [UIView animateWithDuration:0.3 animations:^{
  307. self.slider.sliderBtn.transform = CGAffineTransformMakeScale(1.2, 1.2);
  308. }];
  309. }
  310. /// 滑杆结束滑动
  311. - (void)sliderChangeEnded {
  312. self.slider.isdragging = NO;
  313. [UIView animateWithDuration:0.3 animations:^{
  314. self.slider.sliderBtn.transform = CGAffineTransformIdentity;
  315. }];
  316. }
  317. #pragma mark - getter
  318. - (UIView *)topToolView {
  319. if (!_topToolView) {
  320. _topToolView = [[UIView alloc] init];
  321. UIImage *image = ZFPlayer_Image(@"ZFPlayer_top_shadow");
  322. _topToolView.layer.contents = (id)image.CGImage;
  323. }
  324. return _topToolView;
  325. }
  326. - (UIButton *)backBtn {
  327. if (!_backBtn) {
  328. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  329. [_backBtn setImage:ZFPlayer_Image(@"ZFPlayer_back_full") forState:UIControlStateNormal];
  330. }
  331. return _backBtn;
  332. }
  333. - (UILabel *)titleLabel {
  334. if (!_titleLabel) {
  335. _titleLabel = [[UILabel alloc] init];
  336. _titleLabel.textColor = [UIColor whiteColor];
  337. _titleLabel.font = [UIFont systemFontOfSize:15.0];
  338. }
  339. return _titleLabel;
  340. }
  341. - (UIView *)bottomToolView {
  342. if (!_bottomToolView) {
  343. _bottomToolView = [[UIView alloc] init];
  344. UIImage *image = ZFPlayer_Image(@"ZFPlayer_bottom_shadow");
  345. _bottomToolView.layer.contents = (id)image.CGImage;
  346. }
  347. return _bottomToolView;
  348. }
  349. - (UIButton *)playOrPauseBtn {
  350. if (!_playOrPauseBtn) {
  351. _playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  352. [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_play") forState:UIControlStateNormal];
  353. [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_pause") forState:UIControlStateSelected];
  354. }
  355. return _playOrPauseBtn;
  356. }
  357. - (UILabel *)currentTimeLabel {
  358. if (!_currentTimeLabel) {
  359. _currentTimeLabel = [[UILabel alloc] init];
  360. _currentTimeLabel.textColor = [UIColor whiteColor];
  361. _currentTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  362. _currentTimeLabel.textAlignment = NSTextAlignmentCenter;
  363. }
  364. return _currentTimeLabel;
  365. }
  366. - (ZFSliderView *)slider {
  367. if (!_slider) {
  368. _slider = [[ZFSliderView alloc] init];
  369. _slider.delegate = self;
  370. _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
  371. _slider.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
  372. _slider.minimumTrackTintColor = [UIColor whiteColor];
  373. [_slider setThumbImage:ZFPlayer_Image(@"ZFPlayer_slider") forState:UIControlStateNormal];
  374. _slider.sliderHeight = 2;
  375. }
  376. return _slider;
  377. }
  378. - (UILabel *)totalTimeLabel {
  379. if (!_totalTimeLabel) {
  380. _totalTimeLabel = [[UILabel alloc] init];
  381. _totalTimeLabel.textColor = [UIColor whiteColor];
  382. _totalTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  383. _totalTimeLabel.textAlignment = NSTextAlignmentCenter;
  384. }
  385. return _totalTimeLabel;
  386. }
  387. - (UIButton *)lockBtn {
  388. if (!_lockBtn) {
  389. _lockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  390. [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_unlock-nor") forState:UIControlStateNormal];
  391. [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_lock-nor") forState:UIControlStateSelected];
  392. }
  393. return _lockBtn;
  394. }
  395. @end