MusicHeaderView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // MusicHeaderView.m
  3. // YBVideo
  4. //
  5. // Created by YunBao on 2018/7/28.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "MusicHeaderView.h"
  9. #import "MusicHeaderCell.h"
  10. @interface MusicHeaderView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  11. @property(nonatomic,strong)NSArray *dataArray;
  12. @property(nonatomic,strong)UICollectionView *collectionView;
  13. @property(nonatomic,strong)UIView *segView;
  14. @property(nonatomic,strong)UIButton *hotBtn; //热门top10
  15. @property(nonatomic,strong)UIButton *collBtn; //收藏
  16. @end
  17. @implementation MusicHeaderView
  18. - (instancetype)initWithFrame:(CGRect)frame withBlock:(MusicHeaderBlock)callBack {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.backgroundColor = [UIColor whiteColor];
  22. self.headerEvent = callBack;
  23. self.dataArray = [NSArray array];
  24. [self pullData];
  25. [self addSubview:self.collectionView];
  26. [self addSubview:self.segView];
  27. }
  28. return self;
  29. }
  30. -(void)pullData {
  31. [YBNetworking postWithUrl:@"Music.classify_list" Dic:@{} Suc:^(int code, id info, NSString *msg) {
  32. if (code == 0) {
  33. _dataArray = [NSArray arrayWithArray:info];
  34. [_collectionView reloadData];
  35. }
  36. } Fail:^(id fail) {
  37. }];
  38. }
  39. #pragma mark - 点击事件
  40. -(void)clickHotBtn {
  41. _hotBtn.selected = YES;
  42. _collBtn.selected = NO;
  43. if (self.segEvent) {
  44. self.segEvent(@"热门");
  45. }
  46. }
  47. -(void)clickCollBtn {
  48. _hotBtn.selected = NO;
  49. _collBtn.selected = YES;
  50. if (self.segEvent) {
  51. self.segEvent(@"收藏");
  52. }
  53. }
  54. #pragma mark - CollectionView 代理
  55. /*
  56. * minimumLineSpacing、minimumInteritemSpacing去设置
  57. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  58. return CGSizeMake(0,0);
  59. }
  60. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  61. {
  62. return UIEdgeInsetsMake(0,0,0,0);
  63. }
  64. */
  65. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  66. return 0.01;
  67. }
  68. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  69. return 1;
  70. }
  71. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  72. return _dataArray.count;
  73. }
  74. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  75. MusicHeaderCell *cell = (MusicHeaderCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MusicHeaderCell" forIndexPath:indexPath];
  76. NSDictionary *dic = _dataArray[indexPath.row];
  77. NSString *coverStr = [NSString stringWithFormat:@"%@",[dic valueForKey:@"img_url"]];
  78. [cell.coverIV sd_setImageWithURL:[NSURL URLWithString:coverStr]];
  79. cell.titleL.text = [NSString stringWithFormat:@"%@",[dic valueForKey:@"title"]];
  80. cell.backgroundColor = [UIColor whiteColor];
  81. return cell;
  82. }
  83. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  84. NSDictionary *dic = _dataArray[indexPath.row];
  85. NSString *title = [NSString stringWithFormat:@"%@",[dic valueForKey:@"title"]];
  86. NSString *class_id = [NSString stringWithFormat:@"%@",[dic valueForKey:@"id"]];
  87. self.headerEvent(class_id, title);
  88. }
  89. #pragma mark - set/get
  90. - (UICollectionView *)collectionView {
  91. if (!_collectionView) {
  92. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  93. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  94. flow.itemSize = CGSizeMake(self.width/5.5,self.width/5.5);
  95. flow.minimumLineSpacing = 0;
  96. flow.minimumInteritemSpacing = 0;
  97. flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  98. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,10, self.width, self.width/5.5) collectionViewLayout:flow];
  99. [_collectionView registerNib:[UINib nibWithNibName:@"MusicHeaderCell" bundle:nil] forCellWithReuseIdentifier:@"MusicHeaderCell"];
  100. _collectionView.delegate = self;
  101. _collectionView.dataSource = self;
  102. _collectionView.backgroundColor = [UIColor whiteColor];
  103. _collectionView.showsHorizontalScrollIndicator = NO;
  104. }
  105. return _collectionView;
  106. }
  107. - (UIView *)segView {
  108. if (!_segView) {
  109. _segView = [[UIView alloc]initWithFrame:CGRectMake(0, _collectionView.bottom, self.width, 50)];
  110. _segView.backgroundColor = [UIColor whiteColor];
  111. //热门
  112. _hotBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  113. _hotBtn.frame = CGRectMake(0, 0, self.width/2, 50);
  114. [_hotBtn setTitle:YZMsg(@"热门歌曲") forState:UIControlStateNormal];
  115. _hotBtn.titleLabel.font = SYS_Font(15);
  116. [_hotBtn setImage:[UIImage imageNamed:@"music_topten"] forState:0];
  117. [_hotBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - _hotBtn.imageView.image.size.width, 0, _hotBtn.imageView.image.size.width)];
  118. [_hotBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _hotBtn.titleLabel.bounds.size.width, 0, -_hotBtn.titleLabel.bounds.size.width)];
  119. [_hotBtn setTitleColor:RGB_COLOR(@"#c7c8c9", 1) forState:0];
  120. [_hotBtn setTitleColor:Pink_Cor forState:UIControlStateSelected];
  121. [_hotBtn addTarget:self action:@selector(clickHotBtn) forControlEvents:UIControlEventTouchUpInside];
  122. [_segView addSubview:_hotBtn];
  123. _hotBtn.selected = YES;
  124. //我的收藏
  125. _collBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  126. _collBtn.frame = CGRectMake(self.width/2, 0, self.width/2, 50);
  127. [_collBtn setTitle:YZMsg(@"我的收藏") forState:UIControlStateNormal];
  128. _collBtn.titleLabel.font = SYS_Font(15);
  129. [_collBtn setTitleColor:RGB_COLOR(@"#c7c8c9", 1) forState:0];
  130. [_collBtn setTitleColor:Pink_Cor forState:UIControlStateSelected];
  131. [_collBtn addTarget:self action:@selector(clickCollBtn) forControlEvents:UIControlEventTouchUpInside];
  132. [_segView addSubview:_collBtn];
  133. [PublicObj lineViewWithFrame:CGRectMake(15, _segView.height-1, _segView.width-30, 1) andColor:RGB_COLOR(@"#f4f5f6", 1) andView:_segView];
  134. }
  135. return _segView;
  136. }
  137. @end