XJBottomMenu.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // XJBottomMenu.m
  3. // XJAVPlayer
  4. //
  5. // Created by xj_love on 2016/10/27.
  6. // Copyright © 2016年 Xander. All rights reserved.
  7. //
  8. #import "XJBottomMenu.h"
  9. #import "UIView+SCYCategory.h"
  10. @interface XJBottomMenu (){
  11. BOOL isPlay;
  12. BOOL isHour;
  13. }
  14. @property (nonatomic, strong)UIButton *playOrPauseBtn;//播放/暂停
  15. @property (nonatomic, strong) UIButton *nextPlayerBtn;//下一个视屏(全屏时有)
  16. @property (nonatomic, strong) UIProgressView *loadProgressView;//缓冲进度条
  17. @property (nonatomic, strong) UISlider *playSlider;//播放滑动条
  18. @property (nonatomic, strong) UILabel *timeLabel;//时间标签
  19. @property (nonatomic, strong) UIButton *fullOrSmallBtn;//放大/缩小按钮
  20. @end
  21. @implementation XJBottomMenu
  22. - (instancetype)initWithFrame:(CGRect)frame{
  23. if (self = [super initWithFrame:frame]) {
  24. [self addAllView];
  25. }
  26. return self;
  27. }
  28. - (void)addAllView{
  29. [self addSubview:self.playOrPauseBtn];
  30. [self addSubview:self.nextPlayerBtn];
  31. [self addSubview:self.loadProgressView];
  32. [self addSubview:self.playSlider];
  33. [self addSubview:self.timeLabel];
  34. // [self addSubview:self.fullOrSmallBtn];
  35. }
  36. #pragma mark - **************************** 控件事件 ***********************************
  37. //开始/暂停
  38. - (void)playOrPauseAction{
  39. if (isPlay) {
  40. isPlay = NO;
  41. [self.playOrPauseBtn setImage:[UIImage imageNamed:@"Xjplay"] forState:UIControlStateNormal];
  42. }else{
  43. isPlay = YES;
  44. [self.playOrPauseBtn setImage:[UIImage imageNamed:@"Xjpause"] forState:UIControlStateNormal];
  45. }
  46. if (self.xjPlayOrPauseBlock) {
  47. self.xjPlayOrPauseBlock(isPlay);
  48. }
  49. }
  50. //下一个
  51. - (void)nextPlayerAction{
  52. if (self.xjNextPlayerBlock) {
  53. self.xjNextPlayerBlock();
  54. }
  55. }
  56. //已加载
  57. - (void)setXjLoadedTimeRanges:(CGFloat)xjLoadedTimeRanges{
  58. _xjLoadedTimeRanges = xjLoadedTimeRanges;
  59. [self.loadProgressView setProgress:_xjLoadedTimeRanges animated:YES];
  60. }
  61. //已播放
  62. - (void)setXjCurrentTime:(CGFloat)xjCurrentTime{
  63. _xjCurrentTime = xjCurrentTime;
  64. [self.playSlider setValue:xjCurrentTime animated:YES];
  65. NSString *time1 = [self xjPlayerTimeStyle:xjCurrentTime];
  66. NSString *time2 = [self xjPlayerTimeStyle:self.xjTotalTime];
  67. if (isHour) {
  68. self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",time1,time2];
  69. }else{
  70. self.timeLabel.text = [NSString stringWithFormat:@"00:%@/00:%@",time1,time2];
  71. }
  72. }
  73. //总时长
  74. - (void)setXjTotalTime:(CGFloat)xjTotalTime{
  75. _xjTotalTime = xjTotalTime;
  76. NSString *time = [self xjPlayerTimeStyle:_xjTotalTime];
  77. if (isHour) {
  78. self.timeLabel.text = [NSString stringWithFormat:@"00:00:00/%@",time];
  79. }else{
  80. self.timeLabel.text = [NSString stringWithFormat:@"00:00:00/00:%@",time];
  81. }
  82. self.playSlider.maximumValue = _xjTotalTime;//设置slider的最大值就是总时长
  83. }
  84. //定义视屏时长样式
  85. - (NSString *)xjPlayerTimeStyle:(CGFloat)time{
  86. NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
  87. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  88. if (time/3600>1) {
  89. isHour = YES;
  90. [formatter setDateFormat:@"HH:mm:ss"];
  91. }else{
  92. [formatter setDateFormat:@"mm:ss"];
  93. }
  94. NSString *showTimeStyle = [formatter stringFromDate:date];
  95. return showTimeStyle;
  96. }
  97. //放大/缩小
  98. - (void)fullOrSmallAction{
  99. if (self.xjFull) {
  100. self.xjFull = NO;
  101. }else{
  102. self.xjFull = YES;
  103. }
  104. if (self.xjFullOrSmallBlock) {
  105. self.xjFullOrSmallBlock(self.xjFull);
  106. }
  107. }
  108. //slider拖动时
  109. - (void)playSliderValueChanging:(id)sender{
  110. isPlay = NO;
  111. UISlider *slider = (UISlider*)sender;
  112. if (self.xjSliderValueChangeBlock) {
  113. self.xjSliderValueChangeBlock(slider.value);
  114. }
  115. }
  116. //slider完成拖动时
  117. - (void)playSliderValueDidChanged:(id)sender{
  118. UISlider *slider = (UISlider*)sender;
  119. if (self.xjSliderValueChangeEndBlock) {
  120. self.xjSliderValueChangeEndBlock(slider.value);
  121. }
  122. [self playOrPauseAction];
  123. }
  124. #pragma mark - **************************** 懒加载 *************************************
  125. - (UIButton *)playOrPauseBtn{
  126. if (_playOrPauseBtn == nil) {
  127. _playOrPauseBtn = [[UIButton alloc] init];
  128. [_playOrPauseBtn setImage:[UIImage imageNamed:@"Xjplay"] forState:UIControlStateNormal];
  129. [_playOrPauseBtn addTarget:self action:@selector(playOrPauseAction) forControlEvents:UIControlEventTouchUpInside];
  130. }
  131. return _playOrPauseBtn;
  132. }
  133. - (UIButton *)nextPlayerBtn{
  134. if (_nextPlayerBtn == nil) {
  135. _nextPlayerBtn = [[UIButton alloc] init];
  136. [_nextPlayerBtn setImage:[UIImage imageNamed:@"button_forward"] forState:UIControlStateNormal];
  137. [_nextPlayerBtn addTarget:self action:@selector(nextPlayerAction) forControlEvents:UIControlEventTouchUpInside];
  138. }
  139. return _nextPlayerBtn;
  140. }
  141. - (UIProgressView *)loadProgressView{
  142. if (_loadProgressView == nil) {
  143. _loadProgressView = [[UIProgressView alloc] init];
  144. }
  145. return _loadProgressView;
  146. }
  147. - (UISlider *)playSlider{
  148. if (_playSlider == nil) {
  149. _playSlider = [[UISlider alloc] init];
  150. _playSlider.minimumValue = 0.0;
  151. UIGraphicsBeginImageContextWithOptions((CGSize){1,1}, NO, 0.0f);
  152. UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
  153. UIGraphicsEndImageContext();
  154. [self.playSlider setThumbImage:[UIImage imageNamed:@"icon_progress"] forState:UIControlStateNormal];
  155. [self.playSlider setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
  156. [self.playSlider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
  157. [_playSlider addTarget:self action:@selector(playSliderValueChanging:) forControlEvents:UIControlEventValueChanged];
  158. [_playSlider addTarget:self action:@selector(playSliderValueDidChanged:) forControlEvents:UIControlEventTouchUpInside];
  159. }
  160. return _playSlider;
  161. }
  162. - (UILabel *)timeLabel{
  163. if (_timeLabel == nil) {
  164. _timeLabel = [[UILabel alloc] init];
  165. _timeLabel.textColor = [UIColor whiteColor];
  166. _timeLabel.font = [UIFont systemFontOfSize:11.0];
  167. _timeLabel.textAlignment = NSTextAlignmentCenter;
  168. _timeLabel.text = @"00:00:00/00:00:00";
  169. }
  170. return _timeLabel;
  171. }
  172. - (UIButton *)fullOrSmallBtn{
  173. if (_fullOrSmallBtn == nil) {
  174. _fullOrSmallBtn = [[UIButton alloc] init];
  175. [_fullOrSmallBtn setImage:[UIImage imageNamed:@"big"] forState:UIControlStateNormal];
  176. [_fullOrSmallBtn addTarget:self action:@selector(fullOrSmallAction) forControlEvents:UIControlEventTouchUpInside];
  177. }
  178. return _fullOrSmallBtn;
  179. }
  180. - (void)setXjPlayEnd:(BOOL)xjPlayEnd{
  181. _xjPlayEnd = xjPlayEnd;
  182. if (_xjPlayEnd) {
  183. isPlay = NO;
  184. [self.playOrPauseBtn setImage:[UIImage imageNamed:@"Xjplay"] forState:UIControlStateNormal];
  185. [self.playSlider setValue:0.0 animated:YES];
  186. [self.loadProgressView setProgress:0.0 animated:YES];
  187. NSString *time = [self xjPlayerTimeStyle:self.xjTotalTime];
  188. self.timeLabel.text = [NSString stringWithFormat:@"00:00:00/00:%@",time];
  189. }
  190. }
  191. - (void)setXjPlay:(BOOL)xjPlay{
  192. [self playOrPauseAction];
  193. }
  194. #pragma mark - **************************** 布局 *************************************
  195. - (void)layoutSubviews{
  196. [super layoutSubviews];
  197. self.playOrPauseBtn.frame = CGRectMake(self.left+5, 10, 32, 20);
  198. if (self.xjFull) {
  199. self.nextPlayerBtn.frame = CGRectMake(self.playOrPauseBtn.right, 5, 30, 30);
  200. [_fullOrSmallBtn setImage:[UIImage imageNamed:@"small"] forState:UIControlStateNormal];
  201. }else{
  202. self.nextPlayerBtn.frame = CGRectMake(self.playOrPauseBtn.right+5, 5, 0, 0);
  203. [_fullOrSmallBtn setImage:[UIImage imageNamed:@"big"] forState:UIControlStateNormal];
  204. }
  205. self.fullOrSmallBtn.frame = CGRectMake(self.width-35, 0, 35, self.height);
  206. self.timeLabel.frame = CGRectMake(self.fullOrSmallBtn.left-108, 10, 108, 20);
  207. self.loadProgressView.frame = CGRectMake(self.playOrPauseBtn.right+self.nextPlayerBtn.width+7, 20,self.timeLabel.left-self.playOrPauseBtn.right-self.nextPlayerBtn.width-14, 31);
  208. self.playSlider.frame = CGRectMake(self.playOrPauseBtn.right+self.nextPlayerBtn.width+5, 15, self.loadProgressView.width+4, 10);
  209. }
  210. @end