SoundMixView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // SoundMixView.m
  3. // TXXiaoShiPinDemo
  4. //
  5. // Created by shengcui on 2018/7/23.
  6. // Copyright © 2018年 tencent. All rights reserved.
  7. //
  8. #import "SoundMixView.h"
  9. #define L(x) NSLocalizedString((x),nil)
  10. static NSString * const CellIdentifier = @"cell";
  11. @interface SoundMixView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  12. {
  13. NSMutableArray *_mixEffectArray;
  14. NSMutableArray *_audioEffectArray;
  15. }
  16. @property (strong, nonatomic) IBOutlet UICollectionView *voiceCollectionView;
  17. @property (strong, nonatomic) IBOutlet UICollectionView *mixCollectionView;
  18. @property (strong, nonatomic) IBOutlet UILabel *foiceTitleLabel;
  19. @property (strong, nonatomic) IBOutlet UILabel *mixTitleLabel;
  20. @end
  21. @implementation SoundMixView
  22. + (instancetype)instantiateFromNib
  23. {
  24. UINib *nib = [UINib nibWithNibName:@"SoundMixView" bundle:nil];
  25. return [nib instantiateWithOwner:nil options:nil].lastObject;
  26. }
  27. - (instancetype)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. [self setup];
  32. }
  33. return self;
  34. }
  35. - (void)awakeFromNib
  36. {
  37. [super awakeFromNib];
  38. [self setup];
  39. }
  40. - (void)setup {
  41. self.foiceTitleLabel.text = NSLocalizedString(@"SoundMixView.Foice", nil);
  42. self.mixTitleLabel.text = NSLocalizedString(@"SoundMixView.Mix", nil);
  43. [self setupData];
  44. [self configLayout:self.voiceCollectionView.collectionViewLayout];
  45. [self configLayout:self.mixCollectionView.collectionViewLayout];
  46. UINib *nib = [UINib nibWithNibName:@"LabelCollectionCell" bundle:nil];
  47. [self.voiceCollectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
  48. [self.mixCollectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
  49. self.voiceCollectionView.dataSource = self;
  50. self.mixCollectionView.dataSource = self;
  51. self.voiceCollectionView.delegate = self;
  52. self.mixCollectionView.delegate = self;
  53. NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
  54. [self.voiceCollectionView selectItemAtIndexPath:firstIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  55. [self.mixCollectionView selectItemAtIndexPath:firstIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  56. }
  57. - (void)configLayout:(UICollectionViewLayout *)inLayout {
  58. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout*)inLayout;
  59. CGFloat lineSpacing = [[NSLocale currentLocale].localeIdentifier isEqualToString:@"zh_CN"] ? 12 : 0;
  60. layout.minimumLineSpacing = lineSpacing;
  61. layout.minimumInteritemSpacing = 12;
  62. }
  63. - (void)setupData {
  64. _mixEffectArray = [NSMutableArray arrayWithObjects:L(@"TCVideoRecordMusicView.Origin"),
  65. L(@"TCVideoRecordMusicView.KTV"),
  66. L(@"TCVideoRecordMusicView.Room"),
  67. L(@"TCVideoRecordMusicView.Hall"),
  68. L(@"TCVideoRecordMusicView.Low"),
  69. L(@"TCVideoRecordMusicView.Bright"),
  70. L(@"TCVideoRecordMusicView.Metal"),
  71. L(@"TCVideoRecordMusicView.Magnetic"),
  72. nil];
  73. _audioEffectArray = [NSMutableArray arrayWithObjects:L(@"TCVideoRecordMusicView.Origin"),
  74. L(@"TCVideoRecordMusicView.Child"),
  75. L(@"TCVideoRecordMusicView.Loli"),
  76. L(@"TCVideoRecordMusicView.Uncle"),
  77. L(@"TCVideoRecordMusicView.HeavyMetal"),
  78. // 感冒去掉了
  79. L(@"TCVideoRecordMusicView.Foreigner"),
  80. L(@"TCVideoRecordMusicView.Beast"),
  81. L(@"TCVideoRecordMusicView.Fatty"),
  82. L(@"TCVideoRecordMusicView.StrongCurrent"),
  83. L(@"TCVideoRecordMusicView.HeavyMachinery"),
  84. L(@"TCVideoRecordMusicView.Ethereal"),
  85. nil];
  86. }
  87. - (NSString *)textForView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath
  88. {
  89. if (collectionView == self.voiceCollectionView) {
  90. return _audioEffectArray[indexPath.item];
  91. } else if (collectionView == self.mixCollectionView) {
  92. return _mixEffectArray[indexPath.item];
  93. }
  94. return @"";
  95. }
  96. #pragma mark - UICollectionViewDataSource
  97. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  100. UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
  101. label.text = [self textForView:collectionView indexPath:indexPath];
  102. label.textColor = kBlackColor;
  103. return cell;
  104. }
  105. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  106. if (collectionView == self.voiceCollectionView) {
  107. return _audioEffectArray.count;
  108. } else if (collectionView == self.mixCollectionView) {
  109. return _mixEffectArray.count;
  110. }
  111. return 0;
  112. }
  113. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  114. return 1;
  115. }
  116. #pragma mark - UICollectionViewDelegate
  117. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. if (collectionView == self.voiceCollectionView) {
  120. if ([self.delegate respondsToSelector:@selector(soundMixView:didSelectVoiceChangeIndex:)]) {
  121. [self.delegate soundMixView:self didSelectVoiceChangeIndex:indexPath.item];
  122. }
  123. } else if (collectionView == self.mixCollectionView) {
  124. if ([self.delegate respondsToSelector:@selector(soundMixView:didSelectMixIndex:)]) {
  125. [self.delegate soundMixView:self didSelectMixIndex:indexPath.item];
  126. }
  127. }
  128. }
  129. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
  130. {
  131. NSString *text = [self textForView:collectionView indexPath:indexPath];
  132. CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]}];
  133. size.height = 30;
  134. if (size.width < 44) {
  135. size.width = 44;
  136. }
  137. return size;
  138. }
  139. @end