CWVoiceView.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // CWVoiceView.m
  3. // QQVoiceDemo
  4. //
  5. // Created by 陈旺 on 2017/9/2.
  6. // Copyright © 2017年 陈旺. All rights reserved.
  7. //
  8. #import "CWVoiceView.h"
  9. #import "UIView+CWChat.h"
  10. #import "CWTalkBackView.h"
  11. #import "CWRecordView.h"
  12. #import "CWChangeVoiceView.h"
  13. @interface CWVoiceView ()<UIScrollViewDelegate>
  14. @property (nonatomic,weak) UIScrollView *contentScrollView; // 承载内容的视图
  15. @property (nonatomic,weak) CWTalkBackView *talkBackView; // 对讲视图
  16. @property (nonatomic,weak) CWRecordView *recordView; // 录音视图
  17. @property (nonatomic,weak) CWChangeVoiceView *voiceChangeView; // 变声视图
  18. @property (nonatomic,weak) UIView *smallCirle; // 蓝色小圆点
  19. @property (nonatomic,weak) UIView *bottomView; // 下方(变声、对讲、录音)的view
  20. @property (nonatomic,strong) NSArray *bottomsLabels; // bottomView上的标签数组
  21. @property (nonatomic,weak) UILabel *selectLabel; // 当前选中的label
  22. @end
  23. @implementation CWVoiceView
  24. {
  25. CGFloat _labelDistance;
  26. CGPoint _currentContentOffSize;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame
  29. {
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. self.backgroundColor = [UIColor whiteColor];
  33. [self setupSubViews];
  34. }
  35. return self;
  36. }
  37. - (void)setupSubViews {
  38. // 设置内容滚动视图
  39. [self contentScrollView];
  40. // 设置对讲界面
  41. [self talkBackView];
  42. // 设置录音界面
  43. [self recordView];
  44. // 设置变声界面
  45. [self voiceChangeView];
  46. // 设置下方三个标签界面
  47. [self bottomView];
  48. // 设置标志小圆点
  49. // [self setupSmallCircleView];
  50. _currentContentOffSize = CGPointMake(self.cw_width, 0);
  51. // 设置对讲标签为选中
  52. [self setupSelectLabel:self.bottomsLabels[1]];
  53. }
  54. - (void)setupSelectLabel:(UILabel *)label {
  55. _selectLabel.textColor = kNormalBackGroudColor;
  56. label.textColor = kSelectBackGroudColor;
  57. _selectLabel = label;
  58. }
  59. #pragma mark - subviews
  60. - (UIScrollView *)contentScrollView {
  61. if (_contentScrollView == nil) {
  62. UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.cw_width, self.cw_height)];
  63. scrollV.pagingEnabled = YES;
  64. scrollV.contentSize = CGSizeMake(self.cw_width * 3, 0);
  65. scrollV.contentOffset = CGPointMake(self.cw_width, 0);
  66. scrollV.showsHorizontalScrollIndicator = NO;
  67. scrollV.delegate = self;
  68. [self addSubview:scrollV];
  69. _contentScrollView = scrollV;
  70. _contentScrollView.scrollEnabled = NO;
  71. }
  72. return _contentScrollView;
  73. }
  74. - (CWTalkBackView *)talkBackView {
  75. if (_talkBackView == nil) {
  76. CWTalkBackView *talkView = [[CWTalkBackView alloc] initWithFrame:CGRectMake(0, 0, self.cw_width, self.contentScrollView.cw_height)];
  77. [self.contentScrollView addSubview:talkView];
  78. _talkBackView = talkView;
  79. }
  80. return _talkBackView;
  81. }
  82. - (CWRecordView *)recordView {
  83. if (_recordView == nil) {
  84. CWRecordView *recordView = [[CWRecordView alloc] initWithFrame:CGRectMake(self.cw_width, 0, self.cw_width, self.contentScrollView.cw_height)];
  85. [self.contentScrollView addSubview:recordView];
  86. _recordView = recordView;
  87. }
  88. return _recordView;
  89. }
  90. - (CWChangeVoiceView *)voiceChangeView {
  91. if (_voiceChangeView == nil) {
  92. CWChangeVoiceView *voiceChangeView = [[CWChangeVoiceView alloc] initWithFrame:CGRectMake(0, 0, self.cw_width, self.contentScrollView.cw_height)];
  93. [self.contentScrollView addSubview:voiceChangeView];
  94. _voiceChangeView = voiceChangeView;
  95. }
  96. return _voiceChangeView;
  97. }
  98. - (UIView *)bottomView {
  99. if (_bottomView == nil) {
  100. UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, self.cw_height - 45, self.cw_width, 25)];
  101. [self addSubview:bottomV];
  102. // bottomV.backgroundColor = [UIColor redColor];
  103. _bottomView = bottomV;
  104. [self setupBottomViewSubviews];
  105. }
  106. return _bottomView;
  107. }
  108. - (void)setupBottomViewSubviews {
  109. CGFloat margin = 10;
  110. NSArray *titleArr = @[ASLocalizedString(@"变声"),ASLocalizedString(@"对讲"),ASLocalizedString(@"录音")];
  111. // _bottomsLabels = [NSMutableArray array];
  112. UILabel *talkBackLabel = [self labelWithText:titleArr[1]];
  113. talkBackLabel.center = CGPointMake(self.bottomView.cw_width / 2.0, self.bottomView.cw_height / 2.0);
  114. // talkBackLabel.textColor = kSelectBackGroudColor;
  115. // [self.bottomView addSubview:talkBackLabel];
  116. UILabel *label = [self labelWithText:titleArr[0]];
  117. label.center = CGPointMake(talkBackLabel.cw_left - margin - label.cw_width / 2.0, self.bottomView.cw_height / 2.0);
  118. // [self.bottomView addSubview:label];
  119. UILabel *recordLabel = [self labelWithText:titleArr[2]];
  120. recordLabel.center = CGPointMake(talkBackLabel.cw_right + margin + recordLabel.cw_width / 2.0, self.bottomView.cw_height / 2.0);
  121. [self.bottomView addSubview:recordLabel];
  122. recordLabel.center = CGPointMake(self.centerY, self.bottomView.cw_height / 2.0);
  123. _labelDistance = recordLabel.center.x - talkBackLabel.center.x;
  124. self.bottomsLabels = @[label,talkBackLabel,recordLabel];
  125. }
  126. - (UILabel *)labelWithText:(NSString *)text {
  127. UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  128. label.text = text;
  129. label.textColor = kNormalBackGroudColor;
  130. label.textAlignment = NSTextAlignmentCenter;
  131. label.font = [UIFont systemFontOfSize:14];
  132. [label sizeToFit];
  133. return label;
  134. }
  135. - (void)setupSmallCircleView {
  136. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
  137. view.backgroundColor = kSelectBackGroudColor;
  138. view.layer.cornerRadius = view.cw_width / 2.0;
  139. view.center = CGPointMake(self.cw_width / 2.0, self.bottomView.cw_top - view.cw_height / 2.0);
  140. [self addSubview:view];
  141. self.smallCirle = view;
  142. }
  143. #pragma mark - UIScrollViewDelegate
  144. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  145. CGFloat scrollDistance = scrollView.contentOffset.x - _currentContentOffSize.x;
  146. CGFloat transtionX = scrollDistance / self.contentScrollView.cw_width * _labelDistance;
  147. self.bottomView.transform = CGAffineTransformMakeTranslation(-transtionX, 0);
  148. }
  149. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  150. NSInteger index = scrollView.contentOffset.x / self.contentScrollView.cw_width;
  151. [self setupSelectLabel:self.bottomsLabels[index]];
  152. }
  153. #pragma mark - setter
  154. - (void)setState:(CWVoiceState)state {
  155. _state = state;
  156. self.bottomView.hidden = state != CWVoiceStateDefault;
  157. self.smallCirle.hidden = state != CWVoiceStateDefault;
  158. // self.contentScrollView.scrollEnabled = state == CWVoiceStateDefault;
  159. }
  160. @end