TUIGroupPinCell.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // TUIGroupPinCell.m
  3. // TUIChat
  4. //
  5. // Created by Tencent on 2024/05/20.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIMessageDataProvider.h"
  9. #import "TUIGroupPinCell.h"
  10. @implementation TUIGroupPinCellView
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. [self setupView];
  15. }
  16. return self;
  17. }
  18. - (void)fillWithData:(TUIMessageCellData *)cellData {
  19. self.cellData = cellData;
  20. self.titleLabel.text = [TUIMessageDataProvider getShowName:cellData.innerMessage];
  21. self.content.text = [TUIMessageDataProvider getDisplayString:cellData.innerMessage];
  22. // tell constraints they need updating
  23. [self setNeedsUpdateConstraints];
  24. // update constraints now so we can animate the change
  25. [self updateConstraintsIfNeeded];
  26. [self layoutIfNeeded];
  27. }
  28. + (BOOL)requiresConstraintBasedLayout {
  29. return YES;
  30. }
  31. // this is Apple's recommended place for adding/updating constraints
  32. - (void)updateConstraints {
  33. [super updateConstraints];
  34. [self.leftIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
  35. make.leading.mas_equalTo(self);
  36. make.centerY.mas_equalTo(self);
  37. make.width.mas_equalTo(6);
  38. make.top.bottom.mas_equalTo(self);
  39. }];
  40. [self.titleLabel sizeToFit];
  41. [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  42. make.leading.mas_equalTo(self.leftIcon.mas_trailing).mas_offset(8);
  43. make.trailing.mas_lessThanOrEqualTo(self.removeButton.mas_leading);
  44. make.width.mas_equalTo(self.titleLabel.frame.size.width);
  45. make.height.mas_equalTo(self.titleLabel.frame.size.height);
  46. make.top.mas_equalTo(self).mas_offset(9);
  47. }];
  48. [self.content sizeToFit];
  49. [self.content mas_remakeConstraints:^(MASConstraintMaker *make) {
  50. make.leading.mas_equalTo(self.leftIcon.mas_trailing).mas_offset(8);
  51. make.trailing.mas_lessThanOrEqualTo(self.removeButton.mas_leading);
  52. make.width.mas_equalTo(self.content.frame.size.width);
  53. make.height.mas_equalTo(self.content.frame.size.height);
  54. make.bottom.mas_equalTo(self).mas_offset(-9);
  55. }];
  56. [self.removeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  57. make.trailing.mas_equalTo(-10);
  58. make.centerY.mas_equalTo(self);
  59. make.width.mas_equalTo(30);
  60. make.height.mas_equalTo(30);
  61. }];
  62. [self.removeButton.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  63. make.center.mas_equalTo(self.removeButton);
  64. make.width.mas_equalTo(14);
  65. make.height.mas_equalTo(14);
  66. }];
  67. [self.multiAnimationView mas_remakeConstraints:^(MASConstraintMaker *make) {
  68. make.leading.mas_equalTo(self.mas_leading);
  69. make.trailing.mas_equalTo(self.mas_trailing);
  70. make.width.mas_equalTo(self);
  71. make.height.mas_equalTo(20);
  72. make.top.mas_equalTo(self.mas_bottom);
  73. }];
  74. [self.bottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
  75. make.width.mas_equalTo(self);
  76. make.height.mas_equalTo(0.5);
  77. make.centerX.mas_equalTo(self);
  78. make.bottom.mas_equalTo(self);
  79. }];
  80. }
  81. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  82. CGPoint newP = [self convertPoint:point toView:self.multiAnimationView];
  83. if ([self.multiAnimationView pointInside:newP withEvent:event]) {
  84. return self.multiAnimationView;
  85. }
  86. return [super hitTest:point withEvent:event];
  87. }
  88. - (void)setupView {
  89. self.backgroundColor = TUIChatDynamicColor(@"chat_pop_group_pin_back_color", @"#F9F9F9");
  90. [self addSubview:self.leftIcon];
  91. [self addSubview:self.titleLabel];
  92. [self addSubview:self.content];
  93. [self addSubview:self.removeButton];
  94. [self addSubview:self.multiAnimationView];
  95. [self addSubview:self.bottomLine];
  96. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
  97. [self addGestureRecognizer:tap];
  98. }
  99. - (void)layoutSubviews {
  100. [super layoutSubviews];
  101. }
  102. - (UIImageView *)leftIcon {
  103. if (!_leftIcon) {
  104. _leftIcon = [[UIImageView alloc] init];
  105. _leftIcon.backgroundColor = TUIChatDynamicColor(@"chat_pop_group_pin_left_color", @"#D9D9D9");
  106. }
  107. return _leftIcon;
  108. }
  109. - (UILabel *)titleLabel {
  110. if (!_titleLabel) {
  111. _titleLabel = [[UILabel alloc] init];
  112. _titleLabel.textColor = TUIChatDynamicColor(@"chat_pop_group_pin_title_color", @"#141516");
  113. _titleLabel.font = [UIFont systemFontOfSize:16.0];
  114. }
  115. return _titleLabel;
  116. }
  117. - (UILabel *)content {
  118. if (!_content) {
  119. _content = [[UILabel alloc] init];
  120. _content.textColor = [TUIChatDynamicColor(@"chat_pop_group_pin_subtitle_color", @"#000000")
  121. colorWithAlphaComponent:0.6];
  122. _content.font = [UIFont systemFontOfSize:14.0];
  123. }
  124. return _content;
  125. }
  126. - (UIButton *)removeButton {
  127. if (!_removeButton) {
  128. _removeButton = [UIButton buttonWithType:UIButtonTypeCustom];;
  129. [_removeButton setImage:[UIImage imageNamed:TUIChatImagePath(@"chat_group_del_icon")] forState:UIControlStateNormal];
  130. [_removeButton addTarget:self action:@selector(removeCurrentGroupPin) forControlEvents:UIControlEventTouchUpInside];
  131. }
  132. return _removeButton;
  133. }
  134. - (UIView *)multiAnimationView {
  135. if (!_multiAnimationView) {
  136. _multiAnimationView = [[UIView alloc] initWithFrame:CGRectZero];
  137. _multiAnimationView.alpha = 0;
  138. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
  139. _multiAnimationView.userInteractionEnabled = YES;
  140. [_multiAnimationView addGestureRecognizer:tap];
  141. UIView *arrowBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
  142. arrowBackgroundView.backgroundColor = [UIColor clearColor];
  143. arrowBackgroundView.layer.cornerRadius = 5;
  144. [_multiAnimationView addSubview:arrowBackgroundView];
  145. _multiAnimationView.clipsToBounds = YES;
  146. UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectZero];
  147. arrow.image = TUIChatBundleThemeImage(@"chat_pop_group_pin_down_arrow_img", @"chat_down_arrow_icon");
  148. [arrowBackgroundView addSubview:arrow];
  149. UIView *bottomLine = [[UIView alloc] init];
  150. bottomLine.backgroundColor = [UIColor grayColor];
  151. [arrowBackgroundView addSubview:bottomLine];
  152. [arrowBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.center.mas_equalTo(_multiAnimationView);
  154. make.size.mas_equalTo(CGSizeMake(20, 20));
  155. }];
  156. [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
  157. make.center.mas_equalTo(arrowBackgroundView);
  158. make.size.mas_equalTo(CGSizeMake(20, 20));
  159. }];
  160. [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.width.mas_equalTo(_multiAnimationView);
  162. make.height.mas_equalTo(0.5);
  163. make.centerX.mas_equalTo(_multiAnimationView);
  164. make.bottom.mas_equalTo(_multiAnimationView);
  165. }];
  166. }
  167. return _multiAnimationView;
  168. }
  169. - (UIView *)bottomLine {
  170. if (!_bottomLine) {
  171. _bottomLine = [[UIView alloc] init];
  172. _bottomLine.backgroundColor = [UIColor grayColor];
  173. }
  174. return _bottomLine;
  175. }
  176. - (void)removeCurrentGroupPin {
  177. if (self.onClickRemove) {
  178. self.onClickRemove(self.cellData.innerMessage);
  179. }
  180. }
  181. - (void)onTap:(id)sender {
  182. if (self.onClickCellView) {
  183. self.onClickCellView(self.cellData.innerMessage);
  184. }
  185. }
  186. - (void)hiddenMultiAnimation {
  187. self.multiAnimationView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
  188. _multiAnimationView.alpha = 0;
  189. _bottomLine.alpha = 1;
  190. }
  191. - (void)showMultiAnimation {
  192. self.multiAnimationView.backgroundColor = TUIChatDynamicColor(@"chat_pop_group_pin_back_color", @"#F9F9F9");
  193. _multiAnimationView.alpha = 1;
  194. _bottomLine.alpha = 0;
  195. }
  196. @end
  197. @interface TUIGroupPinCell ()
  198. @property (nonatomic, strong) UIView *separatorView;
  199. @end
  200. @implementation TUIGroupPinCell
  201. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  202. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  203. [self setupView];
  204. }
  205. return self;
  206. }
  207. - (void)setupView {
  208. [self.contentView addSubview:self.cellView];
  209. [self.contentView addSubview:self.separatorView];
  210. }
  211. - (TUIGroupPinCellView *)cellView {
  212. if (!_cellView) {
  213. _cellView = [[TUIGroupPinCellView alloc] init];
  214. _cellView.isFirstPage = NO;
  215. }
  216. return _cellView;
  217. }
  218. - (UIView *)separatorView {
  219. if (!_separatorView) {
  220. _separatorView = [[UIView alloc] init];
  221. _separatorView.backgroundColor = TUIChatDynamicColor(@"chat_pop_group_pin_line_color", @"#DDDDDD");
  222. }
  223. return _separatorView;
  224. }
  225. - (void)fillWithData:(TUIMessageCellData *)cellData {
  226. [self.cellView fillWithData:cellData];
  227. // tell constraints they need updating
  228. [self setNeedsUpdateConstraints];
  229. // update constraints now so we can animate the change
  230. [self updateConstraintsIfNeeded];
  231. [self layoutIfNeeded];
  232. }
  233. + (BOOL)requiresConstraintBasedLayout {
  234. return YES;
  235. }
  236. // this is Apple's recommended place for adding/updating constraints
  237. - (void)updateConstraints {
  238. [super updateConstraints];
  239. [self.cellView mas_remakeConstraints:^(MASConstraintMaker *make) {
  240. make.leading.mas_equalTo(self);
  241. make.trailing.mas_equalTo(self);
  242. make.top.mas_equalTo(self);
  243. make.bottom.mas_equalTo(self);
  244. }];
  245. [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
  246. make.leading.mas_equalTo(self.contentView).mas_offset(6);
  247. make.trailing.mas_equalTo(self.contentView);
  248. make.bottom.mas_equalTo(self.contentView);
  249. make.height.mas_equalTo(0.5);
  250. }];
  251. }
  252. - (void)layoutSubviews {
  253. [super layoutSubviews];
  254. }
  255. @end