Files
yinmeng-ios/xplan-ios/Main/Mine/View/SkillCard/View/SubViews/XPSkillCardSelectPropView.m
2022-12-29 17:15:46 +08:00

250 lines
10 KiB
Objective-C

//
// XPSkillCardSelectPropView.m
// xplan-ios
//
// Created by GreenLand on 2022/1/20.
//
#import "XPSkillCardSelectPropView.h"
///Third
#import <Masonry/Masonry.h>
#import "XPMacro.h"
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "XCHUDTool.h"
#import "NSArray+Safe.h"
// View
#import "XPSkillCardSelectPropCell.h"
// Model
#import "XPSkillCardPropModel.h"
#import "XPSkillCardResourcePropModel.h"
@interface XPSkillCardSelectPropView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *confirmButton;
///选中的子属性名称集合
@property (nonatomic, strong) NSMutableArray *selectPropNameArray;
@end
@implementation XPSkillCardSelectPropView
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(16, 16)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = path.CGPath;
self.layer.mask = maskLayer;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initView];
[self initConstraints];
}
return self;
}
- (void)initView {
self.backgroundColor = [ThemeColor alertBackgroundColor];
[self addSubview:self.titleLabel];
[self addSubview:self.collectionView];
[self addSubview:self.confirmButton];
}
- (void)initConstraints {
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(24);
make.centerX.mas_equalTo(self);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self);
make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(32);
make.bottom.mas_equalTo(self.confirmButton.mas_top).mas_offset(-4);
}];
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.bottom.mas_equalTo(kSafeAreaBottomHeight > 0 ? -kSafeAreaBottomHeight : -5);
make.width.mas_equalTo(240);
make.height.mas_equalTo(40);
}];
}
#pragma mark - UICollectionViewDatasource And UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.sourceArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPSkillCardSelectPropCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPSkillCardSelectPropCell class]) forIndexPath:indexPath];
XPSkillCardSourcePropDetailModel * item = [self.sourceArray safeObjectAtIndex1:indexPath.row];
cell.model = item;
cell.isSelected = [self.selectPropNameArray containsObject:item.propVal];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
XPSkillCardSourcePropDetailModel *item = [self.sourceArray safeObjectAtIndex1:indexPath.row];
if ([self.selectPropNameArray containsObject:item.propVal]) {///取消选中
[self.selectPropNameArray removeObject:item.propVal];
NSInteger index = 0;
for (int i = 0; i<self.selectArray.count; i++) {
XPSkillCardSourcePropDetailModel *selectItem = self.selectArray[i];
if ([selectItem.propVal isEqualToString:item.propVal]) {
index = i;
break;
}
}
if (index < self.selectArray.count) {
[self.selectArray removeObjectAtIndex:index];
}
} else {
XPSkillCardSourcePropDetailModel *propItem = [[XPSkillCardSourcePropDetailModel alloc] init];
propItem.parentId = self.propModel.propId;
propItem.id = item.id;
propItem.propVal = item.propVal;
propItem.refIsOnlyCheck = item.refIsOnlyCheck;
if (self.propModel.state == 0) {//单选
[self.selectArray removeAllObjects];
[self.selectPropNameArray removeAllObjects];
} else if (self.propModel.state == 2) {//多选
if (self.propModel.checkLimitNum == self.selectPropNameArray.count && self.propModel.checkLimitNum > 0) {//达到了多选限制
[XCHUDTool showErrorWithMessage:@"最多可以选择3个哦~"];
return;
}
///擅长路线(全能王和其他的互斥)
if (item.refIsOnlyCheck) {
[self.selectPropNameArray removeAllObjects];
[self.selectArray removeAllObjects];
} else if (self.selectArray.count) {//选择了其他的
NSInteger index = -1;
for (int i = 0; i<self.selectArray.count; i++) {
XPSkillCardSourcePropDetailModel *selectItem = self.selectArray[i];
if (selectItem.refIsOnlyCheck) {
index = i;
break;
}
}
if (index > -1) {
if (index < self.selectArray.count) {
[self.selectArray removeObjectAtIndex:index];
}
if (index < self.selectPropNameArray.count) {
[self.selectPropNameArray removeObjectAtIndex:index];
}
}
}
}
[self.selectPropNameArray addObject:propItem.propVal];
[self.selectArray addObject:propItem];
}
[self.collectionView reloadData];
[self updateConfirmButton];
}
#pragma mark - UICollectionViewFlowlayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 16;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = (KScreenWidth - 30 - 30) / 2;
return CGSizeMake(width, 40);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 30;
}
#pragma mark - event
- (void)onConfirmButtonClick:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPSkillCardSelectPropViewCompleteSelect: editIndex:)]) {
[self.delegate xPSkillCardSelectPropViewCompleteSelect:self.selectArray editIndex:self.editIndex];
}
}
- (void)updateConfirmButton {
if (self.selectArray.count | !self.propModel.isMust) {
[self.confirmButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(50, 30)] forState:UIControlStateNormal];
self.confirmButton.userInteractionEnabled = YES;
} else {
self.confirmButton.userInteractionEnabled = NO;
[self.confirmButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor disableButtonColor], [ThemeColor disableButtonColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(50, 30)] forState:UIControlStateNormal];
}
}
#pragma mark - setter and getter
- (void)setSourceArray:(NSArray<XPSkillCardSourcePropDetailModel *> *)sourceArray {
_sourceArray = sourceArray;
[self.collectionView reloadData];
}
- (void)setSelectArray:(NSMutableArray *)selectArray {
_selectArray = [NSMutableArray arrayWithArray:selectArray];
for (XPSkillCardSourcePropDetailModel *item in selectArray) {
if (item.propVal.length) {
[self.selectPropNameArray addObject:item.propVal];
}
}
[self updateConfirmButton];
}
- (void)setPropModel:(XPSkillCardPropModel *)propModel {
_propModel = propModel;
self.titleLabel.text = propModel.propVal;
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [ThemeColor appCellBackgroundColor];
[_collectionView registerClass:[XPSkillCardSelectPropCell class] forCellWithReuseIdentifier:NSStringFromClass([XPSkillCardSelectPropCell class])];
_collectionView.layer.masksToBounds = YES;
_collectionView.layer.cornerRadius = 10;
}
return _collectionView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:16];
_titleLabel.textColor = [ThemeColor mainTextColor];
}
return _titleLabel;
}
- (UIButton *)confirmButton {
if (!_confirmButton) {
_confirmButton = [[UIButton alloc] init];
[_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
_confirmButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
[_confirmButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(50, 30)] forState:UIControlStateNormal];
[_confirmButton addTarget:self action:@selector(onConfirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
_confirmButton.layer.cornerRadius = 20;
_confirmButton.layer.masksToBounds = YES;
}
return _confirmButton;
}
- (NSMutableArray *)selectPropNameArray {
if (!_selectPropNameArray) {
_selectPropNameArray = [NSMutableArray array];
}
return _selectPropNameArray;
}
@end