ZFPortraitControlView2.m 14 KB

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