// // YOUPAILZChatRoomSetterBgImgWindow.m // MSYOUPAI // // Created by CY on 2022/1/8. // Copyright © 2022 MS. All rights reserved. // #import "YOUPAILZChatRoomSetterBgImgWindow.h" #import "YOUPAILZChatRoomBgImgCell.h" @interface YOUPAILZChatRoomSetterBgImgWindow () @property (nonatomic, weak) UICollectionView *youpaipcollectionView; @property (nonatomic, strong) NSMutableArray *youpaipdataSource; @property (nonatomic, strong) YOUPAILZChatRoomBgImgModel *youpaipSelectedModel; @property (nonatomic,weak)UIButton *youpaipdefineBtn; @end @implementation YOUPAILZChatRoomSetterBgImgWindow - (void)viewDidLoad { [super viewDidLoad]; self.baseView.hidden = YES; [self youpaifinitUI]; [self youpaifrequestBackgoundImageList]; } - (void)youpaifinitUI{ UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, ScaleSize(406.0f))]; [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:bgV size:CGSizeMake(ScaleSize(20.0f), ScaleSize(20.0f))]; bgV.backgroundColor = LCBkgColor; [self.view addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.offset(0.0f); make.height.offset(ScaleSize(406.0f)); }]; UILabel *titleL = [[UILabel alloc] init]; titleL.font = LCBoldFont(ScaleSize(17.0f)); titleL.textColor = [UIColor whiteColor]; titleL.textAlignment = NSTextAlignmentCenter; titleL.text = @"选择背景"; [bgV addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.offset(0.0f); make.height.offset(ScaleSize(58.0f)); }]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = ScaleSize(8.0f); layout.minimumInteritemSpacing = ScaleSize(8.0f); layout.sectionInset = UIEdgeInsetsMake(0.0f, ScaleSize(14.0f), SafeHeight + ScaleSize(16.0f) + 48.0f, ScaleSize(14.0f)); CGFloat width = (KScreenWidth - ScaleSize(44.0f)) / 3.0f; layout.itemSize = CGSizeMake(width, ScaleSize(141.0f)/width*ScaleSize(141.0f)); UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; collectionView.alwaysBounceVertical = YES; collectionView.backgroundColor = LCBkgColor; collectionView.dataSource = self; collectionView.delegate = self; collectionView.scrollEnabled = YES; collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [collectionView registerClass:[YOUPAILZChatRoomBgImgCell class] forCellWithReuseIdentifier:NSStringFromClass(YOUPAILZChatRoomBgImgCell.class)]; [self.view addSubview:collectionView]; self.youpaipcollectionView = collectionView; [collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(0.0f); make.left.right.bottom.offset(0.0f); }]; UIButton *youpaipdefineBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [youpaipdefineBtn setTitle:@"确定" forState:UIControlStateNormal]; [youpaipdefineBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; youpaipdefineBtn.titleLabel.font = LCFont16; youpaipdefineBtn.layer.cornerRadius = 24.0f; youpaipdefineBtn.layer.masksToBounds = YES; youpaipdefineBtn.userInteractionEnabled = NO; [youpaipdefineBtn setBackgroundImage:[LCTools ColorImage:CGSizeMake(KScreenWidth - 80.0f, 48.0f) FromColors:@[HexColorFromRGB(0x4F4B5B),HexColorFromRGB(0x4F4B5B)] ByGradientType:GradientLeftToRight] forState:UIControlStateNormal]; [youpaipdefineBtn addTarget:self action:@selector(youpaipdefineBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:youpaipdefineBtn]; self.youpaipdefineBtn = youpaipdefineBtn; [youpaipdefineBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(40.0f); make.right.offset(-40.0f); make.bottom.offset(-ScaleSize(8.0f)-SafeHeight); make.height.offset(48.0f); }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.youpaipdataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAILZChatRoomBgImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(YOUPAILZChatRoomBgImgCell.class) forIndexPath:indexPath]; [cell youpaifreloadWithModel:self.youpaipdataSource[indexPath.item]]; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.youpaipSelectedModel != nil) { self.youpaipSelectedModel.isSelected = NO; if ([self.youpaipdataSource containsObject:self.youpaipSelectedModel]) { NSInteger item = [self.youpaipdataSource indexOfObject:self.youpaipSelectedModel]; [self.youpaipcollectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:item inSection:0]]]; } } self.youpaipSelectedModel = self.youpaipdataSource[indexPath.item]; self.youpaipSelectedModel.isSelected = YES; if ([self.youpaipdataSource containsObject:self.youpaipSelectedModel]) { NSInteger item = [self.youpaipdataSource indexOfObject:self.youpaipSelectedModel]; [self.youpaipcollectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:item inSection:0]]]; } [self youpaifReloadButtonStyle]; } - (void)youpaifrequestBackgoundImageList{ @weakify(self); [LCHttpHelper requestWithURLString:ChatRoomBackgoundImageList parameters:@{} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 @strongify(self); self.youpaipdataSource = [YOUPAILZChatRoomBgImgModel mj_objectArrayWithKeyValuesArray:dict[@"data"]]; YOUPAILZChatRoomBgImgModel *model = nil; for (NSInteger i = 0; i < self.youpaipdataSource.count; i ++) { YOUPAILZChatRoomBgImgModel *f_model = self.youpaipdataSource[i]; if ([f_model.youpaipid isEqual:self.youpaipSelectedModel.youpaipid]) { f_model.isSelected = YES; model = f_model; break; } } self.youpaipSelectedModel = model; [self youpaifReloadButtonStyle]; [self.youpaipcollectionView reloadData]; } } failure:^(NSError *error) { }]; } - (void)youpaipdefineBtnClick{ @weakify(self); [LCHttpHelper requestWithURLString:ChatRoomSelectedBackgoundImage parameters:@{@"room_id":self.youpaipchatroomModel.youpaiproom_id,@"id":self.youpaipSelectedModel.youpaipid} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { @strongify(self); NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 [self dismissViewControllerAnimated:YES completion:^{}]; } } failure:^(NSError *error) { }]; } - (void)youpaifReloadButtonStyle{ if (self.youpaipSelectedModel == nil) { self.youpaipdefineBtn.userInteractionEnabled = NO; [self.youpaipdefineBtn setBackgroundImage:[LCTools ColorImage:CGSizeMake(KScreenWidth - 80.0f, 48.0f) FromColors:@[HexColorFromRGB(0x4F4B5B),HexColorFromRGB(0x4F4B5B)] ByGradientType:GradientLeftToRight] forState:UIControlStateNormal]; }else{ self.youpaipdefineBtn.userInteractionEnabled = YES; [self.youpaipdefineBtn setBackgroundImage:[LCTools ColorImage:CGSizeMake(KScreenWidth - 80.0f, 48.0f) FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:GradientLeftToRight] forState:UIControlStateNormal]; } } @end