// // 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() @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