123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // JXSelectFriendBottomView.m
- // shiku_im
- //
- // Created by JayLuo on 2020/4/1.
- // Copyright © 2020 Reese. All rights reserved.
- //
- #import "JXSelectFriendBottomView.h"
- #import "JXSelectFriendBottomViewCell.h"
- #import "QCheckBox.h"
- @interface JXSelectFriendBottomView()<UICollectionViewDelegate, UICollectionViewDataSource>
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic) CGRect Gframe;
- @property (nonatomic, strong) NSMutableArray *array;
- @end
- @implementation JXSelectFriendBottomView
- + (JXSelectFriendBottomView *)creatView {
- NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"JXSelectFriendBottomView" owner:nil options:nil];
- JXSelectFriendBottomView *testView = [nibView firstObject];
- return testView;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
-
- NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"JXSelectFriendBottomView" owner:nil options:nil];
- self = [nibView lastObject];
- self.frame = frame;
- _array = [NSMutableArray array];
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%@)", Localized(@"JX_Confirm"),@"0"] forState:UIControlStateNormal];
- [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%@)", Localized(@"JX_Confirm"),@"0"] forState:UIControlStateHighlighted];
- [_finishBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
- [_finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- _finishBtn.layer.masksToBounds = YES;
- _finishBtn.layer.cornerRadius = 3.f;
- [_finishBtn setBackgroundColor:THEMECOLOR];
- CGSize size = [_finishBtn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:SYSFONT(15),NSFontAttributeName,nil]];
- [_finishBtn.titleLabel setFont:SYSFONT(15)];
- _finishBtn.frame = CGRectMake(JX_SCREEN_WIDTH - size.width - 5, 12.5, size.width, 29);
- [_finishBtn addTarget:self action:@selector(onAdd) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_finishBtn];
- // 流水布局
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- // 设置最小行间距
- layout.minimumLineSpacing = 10;
- layout.minimumInteritemSpacing = 0;
- layout.itemSize = CGSizeMake(40, 40);
- UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1) collectionViewLayout:layout];
- collectionView.bounces = NO;
- collectionView.backgroundColor = [UIColor whiteColor];
- collectionView.bounds = CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1);
- collectionView.showsHorizontalScrollIndicator = YES;
- [self addSubview:collectionView];
- // 设置数据源
- collectionView.dataSource = self;
- collectionView.delegate = self;
- _collectionView = collectionView;
- // 注册cell
- [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JXSelectFriendBottomViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([JXSelectFriendBottomViewCell class])];
- [self updateNumber];
- [g_notify addObserver:self selector:@selector(selectuserchange:) name:@"selectuserchange" object:nil];
-
- [g_notify addObserver:self selector:@selector(selectuserchangeUser:) name:@"selectuserchangeUser" object:nil];
-
- }
- - (void)onAdd {
- [[NSNotificationCenter defaultCenter] postNotificationName:@"nextAction" object:nil];
- }
- - (void)selectuserchangeUser:(NSNotification *)note {
-
- _array=note.object;
-
- [_collectionView reloadData];
- [self updateNumber];
-
- return;
- memberData *user = note.object;
- [_array addObject:user];
- if (user.isSelected) {
- [_array addObject:user];
- }else{
- [_array removeObject:user];
- }
- }
- - (void)selectuserchange:(NSNotification *)note {
- JXUserObject *user = note.object;
-
- if (user.isSelected) {
- [_array addObject:user];
- }else{
- [_array removeObject:user];
- }
- [_collectionView reloadData];
- [self updateNumber];
- }
- - (void)updateNumber {
- if (_array.count == 0) {
- _finishBtn.userInteractionEnabled = NO;
- _finishBtn.alpha = 0.4;
- }else {
- _finishBtn.userInteractionEnabled = YES;
- _finishBtn.alpha = 1;
- }
-
- [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", Localized(@"JX_Confirm"),_array.count] forState:UIControlStateNormal];
- [_finishBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", Localized(@"JX_Confirm"),_array.count] forState:UIControlStateHighlighted];
- CGSize size = [_finishBtn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:SYSFONT(15),NSFontAttributeName,nil]];
- [_finishBtn.titleLabel setFont:SYSFONT(15)];
- _finishBtn.frame = CGRectMake(JX_SCREEN_WIDTH - size.width - 20 - 5, 12.5, size.width+20, 29);
- _collectionView.bounds = CGRectMake(0, 1, JX_SCREEN_WIDTH - _finishBtn.frame.size.width - 15, self.frame.size.height-1);
- }
- - (void)dealloc {
- [g_notify removeObserver:self];
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- self.frame = self.Gframe;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return _array.count;
-
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- JXSelectFriendBottomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([JXSelectFriendBottomViewCell class]) forIndexPath:indexPath];
- if ([[_array firstObject] isKindOfClass:[memberData class]]) {
- memberData *user = _array[indexPath.item];
- cell.icon2 = [NSString stringWithFormat:@"%ld",user.userId];
- }else{
- JXUserObject *user = _array[indexPath.item];
- cell.icon = user.userId;
-
- }
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
- return;
-
- JXUserObject *user = _array[indexPath.item];
- user.isSelected = NO;
- user.checkBox.selected = NO;
- [_array removeObject:user];
- [self updateNumber];
- // [collectionView deselectItemAtIndexPath:indexPath animated:YES];
- [collectionView deleteItemsAtIndexPaths:@[indexPath]];
- // 发送通知给bottomCell
- [[NSNotificationCenter defaultCenter] postNotificationName:@"disselectuserchange" object:user];
- }
- -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
-
- // return UIEdgeInsetsZero;
- return UIEdgeInsetsMake(0, 10, 0, 10);
- }
- @end
|