ZFPortraitControlView.m 13 KB

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