JXSelectFriendBottomView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // JXSelectFriendBottomView.m
  3. // shiku_im
  4. //
  5. // Created by JayLuo on 2020/4/1.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXSelectFriendBottomView.h"
  9. #import "JXSelectFriendBottomViewCell.h"
  10. #import "QCheckBox.h"
  11. @interface JXSelectFriendBottomView()<UICollectionViewDelegate, UICollectionViewDataSource>
  12. @property (nonatomic, strong) UICollectionView *collectionView;
  13. @property (nonatomic) CGRect Gframe;
  14. @property (nonatomic, strong) NSMutableArray *array;
  15. @end
  16. @implementation JXSelectFriendBottomView
  17. + (JXSelectFriendBottomView *)creatView {
  18. NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"JXSelectFriendBottomView" owner:nil options:nil];
  19. JXSelectFriendBottomView *testView = [nibView firstObject];
  20. return testView;
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. if (self = [super initWithFrame:frame]) {
  24. NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"JXSelectFriendBottomView" owner:nil options:nil];
  25. self = [nibView lastObject];
  26. self.frame = frame;
  27. _array = [NSMutableArray array];
  28. [self setupUI];
  29. }
  30. return self;
  31. }
  32. - (void)setupUI {
  33. _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  34. [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%@)", Localized(@"JX_Confirm"),@"0"] forState:UIControlStateNormal];
  35. [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%@)", Localized(@"JX_Confirm"),@"0"] forState:UIControlStateHighlighted];
  36. [_finishBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  37. [_finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  38. _finishBtn.layer.masksToBounds = YES;
  39. _finishBtn.layer.cornerRadius = 3.f;
  40. [_finishBtn setBackgroundColor:THEMECOLOR];
  41. CGSize size = [_finishBtn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:SYSFONT(15),NSFontAttributeName,nil]];
  42. [_finishBtn.titleLabel setFont:SYSFONT(15)];
  43. _finishBtn.frame = CGRectMake(JX_SCREEN_WIDTH - size.width - 5, 12.5, size.width, 29);
  44. [_finishBtn addTarget:self action:@selector(onAdd) forControlEvents:UIControlEventTouchUpInside];
  45. [self addSubview:_finishBtn];
  46. // 流水布局
  47. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  48. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  49. // 设置最小行间距
  50. layout.minimumLineSpacing = 10;
  51. layout.minimumInteritemSpacing = 0;
  52. layout.itemSize = CGSizeMake(40, 40);
  53. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1) collectionViewLayout:layout];
  54. collectionView.bounces = NO;
  55. collectionView.backgroundColor = [UIColor whiteColor];
  56. collectionView.bounds = CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1);
  57. collectionView.showsHorizontalScrollIndicator = YES;
  58. [self addSubview:collectionView];
  59. // 设置数据源
  60. collectionView.dataSource = self;
  61. collectionView.delegate = self;
  62. _collectionView = collectionView;
  63. // 注册cell
  64. [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JXSelectFriendBottomViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([JXSelectFriendBottomViewCell class])];
  65. [self updateNumber];
  66. [g_notify addObserver:self selector:@selector(selectuserchange:) name:@"selectuserchange" object:nil];
  67. [g_notify addObserver:self selector:@selector(selectuserchangeUser:) name:@"selectuserchangeUser" object:nil];
  68. }
  69. - (void)onAdd {
  70. [[NSNotificationCenter defaultCenter] postNotificationName:@"nextAction" object:nil];
  71. }
  72. - (void)selectuserchangeUser:(NSNotification *)note {
  73. _array=note.object;
  74. [_collectionView reloadData];
  75. [self updateNumber];
  76. return;
  77. memberData *user = note.object;
  78. [_array addObject:user];
  79. if (user.isSelected) {
  80. [_array addObject:user];
  81. }else{
  82. [_array removeObject:user];
  83. }
  84. }
  85. - (void)selectuserchange:(NSNotification *)note {
  86. JXUserObject *user = note.object;
  87. if (user.isSelected) {
  88. [_array addObject:user];
  89. }else{
  90. [_array removeObject:user];
  91. }
  92. [_collectionView reloadData];
  93. [self updateNumber];
  94. }
  95. - (void)updateNumber {
  96. if (_array.count == 0) {
  97. _finishBtn.userInteractionEnabled = NO;
  98. _finishBtn.alpha = 0.4;
  99. }else {
  100. _finishBtn.userInteractionEnabled = YES;
  101. _finishBtn.alpha = 1;
  102. }
  103. [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", Localized(@"JX_Confirm"),_array.count] forState:UIControlStateNormal];
  104. [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", Localized(@"JX_Confirm"),_array.count] forState:UIControlStateHighlighted];
  105. CGSize size = [_finishBtn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:SYSFONT(15),NSFontAttributeName,nil]];
  106. [_finishBtn.titleLabel setFont:SYSFONT(15)];
  107. _finishBtn.frame = CGRectMake(JX_SCREEN_WIDTH - size.width - 20 - 5, 12.5, size.width+20, 29);
  108. _collectionView.bounds = CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1);
  109. }
  110. - (void)dealloc {
  111. [g_notify removeObserver:self];
  112. }
  113. - (void)awakeFromNib {
  114. [super awakeFromNib];
  115. self.frame = self.Gframe;
  116. }
  117. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  118. return _array.count;
  119. }
  120. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  121. JXSelectFriendBottomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([JXSelectFriendBottomViewCell class]) forIndexPath:indexPath];
  122. if ([[_array firstObject] isKindOfClass:[memberData class]]) {
  123. memberData *user = _array[indexPath.item];
  124. cell.icon2 = [NSString stringWithFormat:@"%ld",user.userId];
  125. }else{
  126. JXUserObject *user = _array[indexPath.item];
  127. cell.icon = user.userId;
  128. }
  129. return cell;
  130. }
  131. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  132. return;
  133. JXUserObject *user = _array[indexPath.item];
  134. user.isSelected = NO;
  135. user.checkBox.selected = NO;
  136. [_array removeObject:user];
  137. [self updateNumber];
  138. // [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  139. [collectionView deleteItemsAtIndexPaths:@[indexPath]];
  140. // 发送通知给bottomCell
  141. [[NSNotificationCenter defaultCenter] postNotificationName:@"disselectuserchange" object:user];
  142. }
  143. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  144. // return UIEdgeInsetsZero;
  145. return UIEdgeInsetsMake(0, 10, 0, 10);
  146. }
  147. @end