ZFPortraitControlView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //
  2. // ZFPortraitControlView.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 "ZFPortraitControlView.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 ZFPortraitControlView () <ZFSliderViewDelegate>
  33. /// 底部工具栏
  34. @property (nonatomic, strong) UIView *bottomToolView;
  35. /// 顶部工具栏
  36. @property (nonatomic, strong) UIView *topToolView;
  37. /// 标题
  38. @property (nonatomic, strong) UILabel *titleLabel;
  39. /// 播放或暂停按钮
  40. @property (nonatomic, strong) UIButton *playOrPauseBtn;
  41. /// 播放的当前时间
  42. @property (nonatomic, strong) UILabel *currentTimeLabel;
  43. /// 滑杆
  44. @property (nonatomic, strong) ZFSliderView *slider;
  45. /// 视频总时间
  46. @property (nonatomic, strong) UILabel *totalTimeLabel;
  47. /// 全屏按钮
  48. @property (nonatomic, strong) UIButton *fullScreenBtn;
  49. @property (nonatomic, assign) BOOL isShow;
  50. @end
  51. @implementation ZFPortraitControlView
  52. - (instancetype)initWithFrame:(CGRect)frame {
  53. if (self = [super initWithFrame:frame]) {
  54. // 添加子控件
  55. [self addSubview:self.topToolView];
  56. [self addSubview:self.bottomToolView];
  57. [self addSubview:self.playOrPauseBtn];
  58. [self.topToolView addSubview:self.titleLabel];
  59. [self.bottomToolView addSubview:self.currentTimeLabel];
  60. [self.bottomToolView addSubview:self.slider];
  61. [self.bottomToolView addSubview:self.totalTimeLabel];
  62. [self.bottomToolView addSubview:self.fullScreenBtn];
  63. // 设置子控件的响应事件
  64. [self makeSubViewsAction];
  65. [self resetControlView];
  66. self.clipsToBounds = YES;
  67. }
  68. return self;
  69. }
  70. - (void)layoutSubviews {
  71. [super layoutSubviews];
  72. CGFloat min_x = 0;
  73. CGFloat min_y = 0;
  74. CGFloat min_w = 0;
  75. CGFloat min_h = 0;
  76. CGFloat min_view_w = self.bounds.size.width;
  77. CGFloat min_view_h = self.bounds.size.height;
  78. CGFloat min_margin = 9;
  79. min_x = 0;
  80. min_y = 0;
  81. min_w = min_view_w;
  82. min_h = 40;
  83. self.topToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  84. min_x = 15;
  85. min_y = 5;
  86. min_w = min_view_w - min_x - 15;
  87. min_h = 30;
  88. self.titleLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  89. min_h = 40;
  90. min_x = 0;
  91. min_y = min_view_h - min_h;
  92. min_w = min_view_w;
  93. self.bottomToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  94. min_x = 0;
  95. min_y = 0;
  96. min_w = 44;
  97. min_h = min_w;
  98. self.playOrPauseBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  99. self.playOrPauseBtn.center = self.center;
  100. min_x = min_margin;
  101. min_w = 62;
  102. min_h = 28;
  103. min_y = (self.bottomToolView.zf_height - min_h)/2;
  104. self.currentTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  105. min_w = 28;
  106. min_h = min_w;
  107. min_x = self.bottomToolView.zf_width - min_w - min_margin;
  108. min_y = 0;
  109. self.fullScreenBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  110. self.fullScreenBtn.zf_centerY = self.currentTimeLabel.zf_centerY;
  111. min_w = 62;
  112. min_h = 28;
  113. min_x = self.fullScreenBtn.zf_left - min_w - 4;
  114. min_y = 0;
  115. self.totalTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  116. self.totalTimeLabel.zf_centerY = self.currentTimeLabel.zf_centerY;
  117. min_x = self.currentTimeLabel.zf_right + 4;
  118. min_y = 0;
  119. min_w = self.totalTimeLabel.zf_left - min_x - 4;
  120. min_h = 30;
  121. self.slider.frame = CGRectMake(min_x, min_y, min_w, min_h);
  122. self.slider.zf_centerY = self.currentTimeLabel.zf_centerY;
  123. if (!self.isShow) {
  124. self.topToolView.zf_y = -self.topToolView.zf_height;
  125. self.bottomToolView.zf_y = self.zf_height;
  126. self.playOrPauseBtn.alpha = 0;
  127. } else {
  128. self.topToolView.zf_y = 0;
  129. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  130. self.playOrPauseBtn.alpha = 1;
  131. }
  132. }
  133. - (void)makeSubViewsAction {
  134. [self.playOrPauseBtn addTarget:self action:@selector(playPauseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  135. [self.fullScreenBtn addTarget:self action:@selector(fullScreenButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  136. }
  137. #pragma mark - action
  138. - (void)playPauseButtonClickAction:(UIButton *)sender {
  139. [self playOrPause];
  140. }
  141. - (void)fullScreenButtonClickAction:(UIButton *)sender {
  142. [self.player enterFullScreen:YES animated:YES];
  143. }
  144. /// 根据当前播放状态取反
  145. - (void)playOrPause {
  146. self.playOrPauseBtn.selected = !self.playOrPauseBtn.isSelected;
  147. self.playOrPauseBtn.isSelected? [self.player.currentPlayerManager play]: [self.player.currentPlayerManager pause];
  148. }
  149. - (void)playBtnSelectedState:(BOOL)selected {
  150. self.playOrPauseBtn.selected = selected;
  151. }
  152. #pragma mark - ZFSliderViewDelegate
  153. - (void)sliderTouchBegan:(float)value {
  154. self.slider.isdragging = YES;
  155. }
  156. - (void)sliderTouchEnded:(float)value {
  157. if (self.player.totalTime > 0) {
  158. self.slider.isdragging = YES;
  159. if (self.sliderValueChanging) self.sliderValueChanging(value, self.slider.isForward);
  160. @weakify(self)
  161. [self.player seekToTime:self.player.totalTime*value completionHandler:^(BOOL finished) {
  162. @strongify(self)
  163. if (finished) {
  164. self.slider.isdragging = NO;
  165. if (self.sliderValueChanged) self.sliderValueChanged(value);
  166. }
  167. }];
  168. if (self.seekToPlay) {
  169. [self.player.currentPlayerManager play];
  170. }
  171. } else {
  172. self.slider.isdragging = NO;
  173. self.slider.value = 0;
  174. }
  175. }
  176. - (void)sliderValueChanged:(float)value {
  177. if (self.player.totalTime == 0) {
  178. self.slider.value = 0;
  179. return;
  180. }
  181. self.slider.isdragging = YES;
  182. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  183. self.currentTimeLabel.text = currentTimeString;
  184. if (self.sliderValueChanging) self.sliderValueChanging(value,self.slider.isForward);
  185. }
  186. - (void)sliderTapped:(float)value {
  187. [self sliderTouchEnded:value];
  188. NSString *currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime*value];
  189. self.currentTimeLabel.text = currentTimeString;
  190. }
  191. #pragma mark - public method
  192. /** 重置ControlView */
  193. - (void)resetControlView {
  194. self.bottomToolView.alpha = 1;
  195. self.slider.value = 0;
  196. self.slider.bufferValue = 0;
  197. self.currentTimeLabel.text = @"00:00";
  198. self.totalTimeLabel.text = @"00:00";
  199. self.backgroundColor = [UIColor clearColor];
  200. self.playOrPauseBtn.selected = YES;
  201. self.titleLabel.text = @"";
  202. }
  203. - (void)showControlView {
  204. self.topToolView.alpha = 1;
  205. self.bottomToolView.alpha = 1;
  206. self.isShow = YES;
  207. self.topToolView.zf_y = 0;
  208. self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height;
  209. self.playOrPauseBtn.alpha = 1;
  210. self.player.statusBarHidden = NO;
  211. }
  212. - (void)hideControlView {
  213. self.isShow = NO;
  214. self.topToolView.zf_y = -self.topToolView.zf_height;
  215. self.bottomToolView.zf_y = self.zf_height;
  216. self.player.statusBarHidden = NO;
  217. self.playOrPauseBtn.alpha = 0;
  218. self.topToolView.alpha = 0;
  219. self.bottomToolView.alpha = 0;
  220. }
  221. - (BOOL)shouldResponseGestureWithPoint:(CGPoint)point withGestureType:(ZFPlayerGestureType)type touch:(nonnull UITouch *)touch {
  222. CGRect sliderRect = [self.bottomToolView convertRect:self.slider.frame toView:self];
  223. if (CGRectContainsPoint(sliderRect, point)) {
  224. return NO;
  225. }
  226. return YES;
  227. }
  228. - (void)videoPlayer:(ZFPlayerController *)videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime {
  229. if (!self.slider.isdragging) {
  230. NSString *currentTimeString = [ZFUtilities convertTimeSecond:currentTime];
  231. self.currentTimeLabel.text = currentTimeString;
  232. NSString *totalTimeString = [ZFUtilities convertTimeSecond:totalTime];
  233. self.totalTimeLabel.text = totalTimeString;
  234. self.slider.value = videoPlayer.progress;
  235. }
  236. }
  237. - (void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime {
  238. self.slider.bufferValue = videoPlayer.bufferProgress;
  239. }
  240. - (void)showTitle:(NSString *)title fullScreenMode:(ZFFullScreenMode)fullScreenMode {
  241. self.titleLabel.text = title;
  242. self.player.orientationObserver.fullScreenMode = fullScreenMode;
  243. }
  244. /// 调节播放进度slider和当前时间更新
  245. - (void)sliderValueChanged:(CGFloat)value currentTimeString:(NSString *)timeString {
  246. self.slider.value = value;
  247. self.currentTimeLabel.text = timeString;
  248. self.slider.isdragging = YES;
  249. [UIView animateWithDuration:0.3 animations:^{
  250. self.slider.sliderBtn.transform = CGAffineTransformMakeScale(1.2, 1.2);
  251. }];
  252. }
  253. /// 滑杆结束滑动
  254. - (void)sliderChangeEnded {
  255. self.slider.isdragging = NO;
  256. [UIView animateWithDuration:0.3 animations:^{
  257. self.slider.sliderBtn.transform = CGAffineTransformIdentity;
  258. }];
  259. }
  260. #pragma mark - getter
  261. - (UIView *)topToolView {
  262. if (!_topToolView) {
  263. _topToolView = [[UIView alloc] init];
  264. UIImage *image = ZFPlayer_Image(@"ZFPlayer_top_shadow");
  265. _topToolView.layer.contents = (id)image.CGImage;
  266. }
  267. return _topToolView;
  268. }
  269. - (UILabel *)titleLabel {
  270. if (!_titleLabel) {
  271. _titleLabel = [[UILabel alloc] init];
  272. _titleLabel.textColor = [UIColor whiteColor];
  273. _titleLabel.font = [UIFont systemFontOfSize:15.0];
  274. }
  275. return _titleLabel;
  276. }
  277. - (UIView *)bottomToolView {
  278. if (!_bottomToolView) {
  279. _bottomToolView = [[UIView alloc] init];
  280. UIImage *image = ZFPlayer_Image(@"ZFPlayer_bottom_shadow");
  281. _bottomToolView.layer.contents = (id)image.CGImage;
  282. }
  283. return _bottomToolView;
  284. }
  285. - (UIButton *)playOrPauseBtn {
  286. if (!_playOrPauseBtn) {
  287. _playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  288. [_playOrPauseBtn setImage:ZFPlayer_Image(@"new_allPlay_44x44_") forState:UIControlStateNormal];
  289. [_playOrPauseBtn setImage:ZFPlayer_Image(@"new_allPause_44x44_") forState:UIControlStateSelected];
  290. }
  291. return _playOrPauseBtn;
  292. }
  293. - (UILabel *)currentTimeLabel {
  294. if (!_currentTimeLabel) {
  295. _currentTimeLabel = [[UILabel alloc] init];
  296. _currentTimeLabel.textColor = [UIColor whiteColor];
  297. _currentTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  298. _currentTimeLabel.textAlignment = NSTextAlignmentCenter;
  299. }
  300. return _currentTimeLabel;
  301. }
  302. - (ZFSliderView *)slider {
  303. if (!_slider) {
  304. _slider = [[ZFSliderView alloc] init];
  305. _slider.delegate = self;
  306. _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
  307. _slider.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
  308. _slider.minimumTrackTintColor = [UIColor whiteColor];
  309. [_slider setThumbImage:ZFPlayer_Image(@"ZFPlayer_slider") forState:UIControlStateNormal];
  310. _slider.sliderHeight = 2;
  311. }
  312. return _slider;
  313. }
  314. - (UILabel *)totalTimeLabel {
  315. if (!_totalTimeLabel) {
  316. _totalTimeLabel = [[UILabel alloc] init];
  317. _totalTimeLabel.textColor = [UIColor whiteColor];
  318. _totalTimeLabel.font = [UIFont systemFontOfSize:14.0f];
  319. _totalTimeLabel.textAlignment = NSTextAlignmentCenter;
  320. }
  321. return _totalTimeLabel;
  322. }
  323. - (UIButton *)fullScreenBtn {
  324. if (!_fullScreenBtn) {
  325. _fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  326. [_fullScreenBtn setImage:ZFPlayer_Image(@"ZFPlayer_fullscreen") forState:UIControlStateNormal];
  327. }
  328. return _fullScreenBtn;
  329. }
  330. @end