BGSoundEffectsView.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // BGSoundEffectsView.m
  3. // BuguLive
  4. //
  5. // Created by bugu on 2019/12/11.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "BGSoundEffectsView.h"
  9. #import "BGSoundEffectCell.h"
  10. @interface BGSoundEffectsView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  11. @property(nonatomic, strong) UIView *shadowView;
  12. @property(nonatomic, strong) UIView *bgView;
  13. @property(nonatomic, strong) UILabel *titleLabel;
  14. @property(nonatomic, strong) QMUIButton *addSoundEffectBtn;
  15. @property(nonatomic, strong) UICollectionView *collectionView;
  16. @property(nonatomic, strong) NSMutableArray *dataArray;
  17. @end
  18. @implementation BGSoundEffectsView
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. [self initUI];
  24. //[self requestData];
  25. }
  26. return self;
  27. }
  28. - (void)initUI{
  29. _bgView=({
  30. UIView * view = [[UIView alloc]init];
  31. view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.75];
  32. view.layer.cornerRadius = 9;
  33. view;
  34. });
  35. _titleLabel= ({
  36. UILabel * label = [[UILabel alloc]init];
  37. label.textColor = kWhiteColor;
  38. label.font = [UIFont systemFontOfSize:16];
  39. label.text = ASLocalizedString(@"音效");
  40. label;
  41. });
  42. _addSoundEffectBtn = ({
  43. QMUIButton * btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  44. [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  45. btn.titleLabel.font = [UIFont systemFontOfSize:16];
  46. [btn setTitle:ASLocalizedString(@"上传")forState:UIControlStateNormal];
  47. [btn setImage:[UIImage imageNamed:@"lr上传"] forState:UIControlStateNormal];
  48. btn.imagePosition = QMUIButtonImagePositionLeft;
  49. btn.spacingBetweenImageAndTitle = 2;
  50. [btn addTarget:self action:@selector(uploadAction) forControlEvents:UIControlEventTouchUpInside];
  51. btn;
  52. });
  53. _addSoundEffectBtn.hidden = YES;
  54. [self addSubview:_bgView];
  55. [_bgView addSubview:_titleLabel];
  56. [_bgView addSubview:_addSoundEffectBtn];
  57. [_bgView addSubview:self.collectionView];
  58. }
  59. - (void)requestData{
  60. self.dataArray = [NSMutableArray array];
  61. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  62. [parmDict setObject:@"index" forKey:@"ctl"];
  63. [parmDict setObject:@"home_sound" forKey:@"act"];
  64. FWWeakify(self)
  65. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  66. NSArray * array = responseJson[@"data"];
  67. for (id obj in array)
  68. {
  69. BGSoundEffectModel *model =[BGSoundEffectModel mj_objectWithKeyValues:obj];
  70. [self.dataArray addObject:model];
  71. }
  72. [self.collectionView reloadData];
  73. } FailureBlock:^(NSError *error) {
  74. }];
  75. }
  76. - (void)uploadAction {
  77. if (self.uploadBlock) {
  78. self.uploadBlock();
  79. }
  80. }
  81. - (void)layoutSubviews {
  82. [super layoutSubviews];
  83. [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.top.mas_equalTo(0);
  85. make.left.right.bottom.mas_equalTo(0);
  86. }];
  87. [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.mas_equalTo(23);
  89. make.centerX.mas_equalTo(0);
  90. }];
  91. [_addSoundEffectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.centerY.equalTo(_titleLabel);
  93. make.right.mas_equalTo(-12);
  94. make.width.mas_equalTo(72);
  95. }];
  96. }
  97. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  98. return 1;
  99. }
  100. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  101. // return 4;
  102. return self.dataArray.count;
  103. }
  104. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  105. BGSoundEffectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BGSoundEffectCell class]) forIndexPath:indexPath];
  106. cell.model = self.dataArray[indexPath.item];
  107. return cell;
  108. }
  109. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  110. // BGSoundEffectModel *model = self.dataArray[indexPath.item];
  111. // NSDictionary * attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
  112. // CGFloat labelWidth = [model.name boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width + 60;
  113. //
  114. return CGSizeMake( kScreenW / 4 + kRealValue(10) , kRealValue(40)+10);
  115. }
  116. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  117. return 5;
  118. }
  119. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  120. return 5;
  121. }
  122. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  123. return UIEdgeInsetsMake(0, 5, 0, 5);
  124. // if (section == 0) {
  125. // return UIEdgeInsetsMake(0, 14, 0, 14);
  126. //
  127. // }
  128. //
  129. // return UIEdgeInsetsMake(0, 59, 0, 59);
  130. }
  131. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  132. //2020-1-2 点击播放某个音效
  133. // BGSoundEffectCell *cell = (BGSoundEffectCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
  134. // cell.titleLabel.textColor=kRedColor;
  135. BGSoundEffectModel *model=self.dataArray[indexPath.item];
  136. __weak typeof(self) weakself =self;
  137. if(self.playUrl)
  138. {
  139. self.playUrl(model);
  140. }
  141. [self hide];
  142. // [self addVideoWithPlayerLayerFrame:CGRectZero withPlayerItemUrlString:model.url complete:^(AVPlayer *player) {
  143. // if(self.playUrl)
  144. // {
  145. // self.playUrl(model.url);
  146. // }
  147. //// weakself.player =player;
  148. //// [weakself.player play];
  149. // [weakself hide];
  150. // }];
  151. }
  152. #pragma mark - Show And Hide
  153. - (void)show:(UIView *)superView{
  154. [self requestData];
  155. [superView addSubview:self.shadowView];
  156. [superView addSubview:self];
  157. [UIView animateWithDuration:0.25 animations:^{
  158. self.shadowView.alpha = 1;
  159. self.y = kScreenH / 2;
  160. }];
  161. }
  162. - (void)hide{
  163. [UIView animateWithDuration:0.25 animations:^{
  164. self.shadowView.alpha = 1;
  165. self.y = kScreenH;
  166. } completion:^(BOOL finished) {
  167. [self.shadowView removeFromSuperview];
  168. [self removeFromSuperview];
  169. }];
  170. }
  171. - (UIView *)shadowView{
  172. if (!_shadowView) {
  173. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  174. _shadowView.backgroundColor = kClearColor;
  175. _shadowView.userInteractionEnabled = YES;
  176. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  177. [_shadowView addGestureRecognizer:tap];
  178. }
  179. return _shadowView;
  180. }
  181. #pragma mark - setter
  182. - (UICollectionView *)collectionView{
  183. if (!_collectionView) {
  184. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  185. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,70, kScreenW , kScreenH/2-70) collectionViewLayout:layout];
  186. _collectionView.delegate = self;
  187. _collectionView.backgroundColor = [UIColor clearColor];
  188. _collectionView.dataSource = self;
  189. [_collectionView registerClass:[BGSoundEffectCell class] forCellWithReuseIdentifier:NSStringFromClass([BGSoundEffectCell class])];
  190. }
  191. return _collectionView;
  192. }
  193. @end