|
@@ -0,0 +1,357 @@
|
|
|
+//
|
|
|
+// TenantGroupSortView.m
|
|
|
+// KulexiuForTeacher
|
|
|
+//
|
|
|
+// Created by 王智 on 2023/10/19.
|
|
|
+//
|
|
|
+
|
|
|
+#import "TenantGroupSortView.h"
|
|
|
+
|
|
|
+@interface TenantGroupSortView ()<UIGestureRecognizerDelegate>
|
|
|
+
|
|
|
+@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
|
|
|
+
|
|
|
+@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeight;
|
|
|
+
|
|
|
+@property (weak, nonatomic) IBOutlet UIView *bgView;
|
|
|
+
|
|
|
+@property (weak, nonatomic) IBOutlet UIView *bottomView;
|
|
|
+
|
|
|
+@property (nonatomic, strong) UIScrollView *containerScrollView;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSMutableArray *subjectArray;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSMutableArray *groupArray;
|
|
|
+
|
|
|
+@property (nonatomic, assign) NSInteger subjectChooseIndex;
|
|
|
+
|
|
|
+@property (nonatomic, assign) NSInteger groupChooseIndex;
|
|
|
+
|
|
|
+@property (nonatomic, copy) TenantSortCallback callback;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation TenantGroupSortView
|
|
|
+
|
|
|
+- (void)awakeFromNib {
|
|
|
+ [super awakeFromNib];
|
|
|
+ [self configDefaultUI];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)configDefaultUI {
|
|
|
+ if (IS_iPhoneX) {
|
|
|
+ self.bottomHeight.constant = 25 + 44 + iPhoneXSafeBottomMargin;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ self.bottomHeight.constant = 25 + 44 + 20;
|
|
|
+ }
|
|
|
+ [self.bgView addSubview:self.containerScrollView];
|
|
|
+ [self.containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.right.mas_equalTo(self.bgView);
|
|
|
+ make.top.mas_equalTo(self.bgView.mas_top).offset(69);
|
|
|
+ make.bottom.mas_equalTo(self.bgView.mas_bottom);
|
|
|
+ }];
|
|
|
+ self.bgView.layer.cornerRadius = 20;
|
|
|
+ self.bgView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
|
|
|
+
|
|
|
+ UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
|
|
|
+ gesture.delegate = self;
|
|
|
+ [self addGestureRecognizer:gesture];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)sortAction:(TenantSortCallback)callback {
|
|
|
+ if (callback) {
|
|
|
+ self.callback = callback;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
++ (instancetype)shareInstance {
|
|
|
+ TenantGroupSortView *view = [[[NSBundle mainBundle] loadNibNamed:@"TenantGroupSortView" owner:nil options:nil] firstObject];
|
|
|
+ return view;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)configSubjectArray:(NSMutableArray *)subjectArray groupArray:(NSMutableArray *)groupArray {
|
|
|
+ self.subjectArray = [subjectArray mutableCopy];
|
|
|
+ self.groupArray = [groupArray mutableCopy];
|
|
|
+ [self configUI];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)configUI {
|
|
|
+ [self.containerScrollView removeAllSubViews];
|
|
|
+ CGFloat topHeight = 0;
|
|
|
+
|
|
|
+ UILabel *subjectTitle = [self createLabel:@"声部"];
|
|
|
+ [self.containerScrollView addSubview:subjectTitle];
|
|
|
+ [subjectTitle mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.mas_equalTo(self.mas_left).offset(16);
|
|
|
+ make.height.mas_equalTo(22.0f);
|
|
|
+ make.top.mas_equalTo(self.containerScrollView.mas_top);
|
|
|
+ }];
|
|
|
+ topHeight += 22.0f;
|
|
|
+ CGFloat midSpace = 8.0f;
|
|
|
+ CGFloat leftSpace = 16.0f;
|
|
|
+ CGFloat topSpace = 12.0f;
|
|
|
+ CGFloat buttonHeight = 32.0f;
|
|
|
+ CGFloat minWidth = 70.0f;
|
|
|
+ CGFloat maxWidth = KPortraitWidth - 16 - 15;
|
|
|
+ CGFloat xSpace = leftSpace;
|
|
|
+ CGFloat ySpace = topHeight + 14.0f;
|
|
|
+ for (NSInteger index = 0; index < self.subjectArray.count; index++) {
|
|
|
+ NSDictionary *parm = self.subjectArray[index];
|
|
|
+ NSString *title = [parm ks_stringValueForKey:@"name"];
|
|
|
+ UIButton *button = [self createButton:1000+index buttonTitle:title];
|
|
|
+ [self.containerScrollView addSubview:button];
|
|
|
+ if (self.subjectChooseIndex == 1000 + index) {
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:1000+index];
|
|
|
+ }
|
|
|
+ CGFloat buttonWidth = [self getWidthWithString:title];
|
|
|
+ // 按钮宽度最小和最大限定
|
|
|
+ if (buttonWidth < minWidth) {
|
|
|
+ buttonWidth = minWidth;
|
|
|
+ }
|
|
|
+ else if (buttonWidth > maxWidth) {
|
|
|
+ buttonWidth = maxWidth;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (xSpace + buttonWidth - 16 > maxWidth) {
|
|
|
+ xSpace = leftSpace;
|
|
|
+ ySpace = ySpace + topSpace + buttonHeight;
|
|
|
+ }
|
|
|
+ [button mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.mas_equalTo(self.bgView.mas_left).offset(xSpace);
|
|
|
+ make.width.mas_equalTo(buttonWidth);
|
|
|
+ make.height.mas_equalTo(buttonHeight);
|
|
|
+ make.top.mas_equalTo(subjectTitle.mas_bottom).offset(ySpace);
|
|
|
+ }];
|
|
|
+
|
|
|
+ xSpace += buttonWidth + midSpace;
|
|
|
+ }
|
|
|
+ xSpace = leftSpace;
|
|
|
+ topHeight = ySpace + 32 + 28;
|
|
|
+
|
|
|
+ // 小组
|
|
|
+ UILabel *groupTitle = [self createLabel:@"小组"];
|
|
|
+ [self.containerScrollView addSubview:groupTitle];
|
|
|
+ [groupTitle mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.mas_equalTo(self.mas_left).offset(16);
|
|
|
+ make.height.mas_equalTo(22.0f);
|
|
|
+ make.top.mas_equalTo(subjectTitle.mas_bottom).offset(topHeight);
|
|
|
+ }];
|
|
|
+ topHeight += 22.0f;
|
|
|
+ ySpace = topHeight + 20;
|
|
|
+ for (NSInteger index = 0; index < self.groupArray.count; index++) {
|
|
|
+ NSDictionary *parm = self.groupArray[index];
|
|
|
+ NSString *title = [parm ks_stringValueForKey:@"name"];
|
|
|
+ UIButton *button = [self createButton:2000+index buttonTitle:title];
|
|
|
+ [self.containerScrollView addSubview:button];
|
|
|
+ if (self.groupChooseIndex == 2000 + index) {
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:2000+index];
|
|
|
+ }
|
|
|
+
|
|
|
+ CGFloat buttonWidth = [self getWidthWithString:title];
|
|
|
+ // 按钮宽度最小和最大限定
|
|
|
+ if (buttonWidth < minWidth) {
|
|
|
+ buttonWidth = minWidth;
|
|
|
+ }
|
|
|
+ else if (buttonWidth > maxWidth) {
|
|
|
+ buttonWidth = maxWidth;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (xSpace + buttonWidth - 16 > maxWidth) {
|
|
|
+ xSpace = leftSpace;
|
|
|
+ ySpace = ySpace + topSpace + buttonHeight;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (index == self.groupArray.count - 1) {
|
|
|
+ [button mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.mas_equalTo(self.bgView.mas_left).offset(xSpace);
|
|
|
+ make.width.mas_equalTo(buttonWidth);
|
|
|
+ make.height.mas_equalTo(buttonHeight);
|
|
|
+ make.top.mas_equalTo(subjectTitle.mas_bottom).offset(ySpace);
|
|
|
+ make.bottom.mas_equalTo(self.containerScrollView.mas_bottom).offset(-28);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [button mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.mas_equalTo(self.bgView.mas_left).offset(xSpace);
|
|
|
+ make.width.mas_equalTo(buttonWidth);
|
|
|
+ make.height.mas_equalTo(buttonHeight);
|
|
|
+ make.top.mas_equalTo(subjectTitle.mas_bottom).offset(ySpace);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ xSpace += buttonWidth + midSpace;
|
|
|
+ }
|
|
|
+ topHeight = ySpace + 28;
|
|
|
+
|
|
|
+ CGFloat viewHeight = topHeight + 69;
|
|
|
+ if (viewHeight> KPortraitHeight - kNaviBarHeight - self.bottomHeight.constant) {
|
|
|
+ self.viewHeight.constant = KPortraitHeight - kNaviBarHeight - self.bottomHeight.constant;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ self.viewHeight.constant = topHeight;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat)getWidthWithString:(NSString *)name {
|
|
|
+ CGFloat width = [name boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} context:nil].size.width + 16;
|
|
|
+ return width;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (void)modifyButtonStatus:(BOOL)isChoose buttonIndex:(NSInteger)buttonIndex {
|
|
|
+ UIButton *button = (UIButton *)[self.containerScrollView viewWithTag:buttonIndex];
|
|
|
+ if (isChoose) {
|
|
|
+ [button setBackgroundColor:HexRGB(0xF0FFFC)];
|
|
|
+ [button setTitleColor:HexRGB(0x2DC7AA) forState:UIControlStateNormal];
|
|
|
+ button.layer.borderColor = HexRGB(0x2DC7AA).CGColor;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [button setBackgroundColor:HexRGB(0xF2F2F2)];
|
|
|
+ [button setTitleColor:HexRGB(0xAAAAAA) forState:UIControlStateNormal];
|
|
|
+ button.layer.borderColor = [UIColor clearColor].CGColor;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)showInView:(UIView *)displayView {
|
|
|
+ [displayView addSubview:self];
|
|
|
+ [self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.left.right.top.bottom.mas_equalTo(displayView);
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)hideView {
|
|
|
+ [self removeFromSuperview];
|
|
|
+}
|
|
|
+
|
|
|
+- (UILabel *)createLabel:(NSString *)title {
|
|
|
+ UILabel *label = [[UILabel alloc] init];
|
|
|
+ label.text = title;
|
|
|
+ label.textColor = HexRGB(0x333333);
|
|
|
+ label.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightRegular];
|
|
|
+ return label;
|
|
|
+}
|
|
|
+
|
|
|
+- (UIButton *)createButton:(NSInteger)tag buttonTitle:(NSString *)buttonTitle {
|
|
|
+ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
+ button.tag = tag;
|
|
|
+ [button setTitle:buttonTitle forState:UIControlStateNormal];
|
|
|
+ [button.titleLabel setFont:[UIFont systemFontOfSize:14.0f weight:UIFontWeightRegular]];
|
|
|
+ button.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
|
|
+ [button setTitleColor:HexRGB(0xAAAAAA) forState:UIControlStateNormal];
|
|
|
+ [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ button.layer.cornerRadius = 16.0f;
|
|
|
+ button.backgroundColor = HexRGB(0xF2F2F2);
|
|
|
+ button.layer.borderWidth = 1.0f;
|
|
|
+ button.layer.borderColor = [UIColor clearColor].CGColor;
|
|
|
+ return button;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)buttonClickAction:(UIButton *)sender {
|
|
|
+ if (sender.tag >= 1000 && sender.tag < 2000) {
|
|
|
+ if (self.subjectChooseIndex > 0) {
|
|
|
+ if (self.subjectChooseIndex == sender.tag) {
|
|
|
+ self.subjectChooseIndex = 0;
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:sender.tag];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:self.subjectChooseIndex];
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:sender.tag];
|
|
|
+ self.subjectChooseIndex = sender.tag;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ self.subjectChooseIndex = sender.tag;
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:sender.tag];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (sender.tag >= 2000 && sender.tag < 3000) { // 小组
|
|
|
+ if (self.groupChooseIndex > 0) {
|
|
|
+ if (self.groupChooseIndex == sender.tag) {
|
|
|
+ self.groupChooseIndex = 0;
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:sender.tag];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:self.groupChooseIndex];
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:sender.tag];
|
|
|
+ self.groupChooseIndex = sender.tag;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ self.groupChooseIndex = sender.tag;
|
|
|
+ [self modifyButtonStatus:YES buttonIndex:sender.tag];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (IBAction)cancleAction:(id)sender {
|
|
|
+ [self hideView];
|
|
|
+}
|
|
|
+
|
|
|
+- (IBAction)resetAction:(id)sender {
|
|
|
+ [self resetView];
|
|
|
+ self.callback(SORTACTION_SEARCH, @{});
|
|
|
+}
|
|
|
+- (void)resetView {
|
|
|
+ if (self.subjectChooseIndex != 0) {
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:self.subjectChooseIndex];
|
|
|
+ self.subjectChooseIndex = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self.groupChooseIndex != 0) {
|
|
|
+ [self modifyButtonStatus:NO buttonIndex:self.groupChooseIndex];
|
|
|
+ self.groupChooseIndex = 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+- (IBAction)checkAction:(id)sender {
|
|
|
+ [self hideView];
|
|
|
+ if (self.callback) {
|
|
|
+ NSMutableDictionary *parm = [NSMutableDictionary dictionary];
|
|
|
+ if (self.subjectChooseIndex != 0) {
|
|
|
+ NSDictionary *subjectParm = self.subjectArray[self.subjectChooseIndex - 1000];
|
|
|
+ [parm setValue:[subjectParm ks_stringValueForKey:@"id"] forKey:@"subjectId"];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [parm setValue:@"" forKey:@"subjectId"];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self.groupChooseIndex != 0) {
|
|
|
+ NSDictionary *roleParm = self.groupArray[self.groupChooseIndex - 2000];
|
|
|
+ [parm setValue:[roleParm ks_stringValueForKey:@"id"] forKey:@"groupId"];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [parm setValue:@"" forKey:@"groupId"];
|
|
|
+ }
|
|
|
+ self.callback(SORTACTION_SEARCH, parm);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (UIScrollView *)containerScrollView {
|
|
|
+ if (!_containerScrollView) {
|
|
|
+ _containerScrollView = [[UIScrollView alloc] init];
|
|
|
+ }
|
|
|
+ return _containerScrollView;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)tapAction:(UITapGestureRecognizer *)gesture {
|
|
|
+ [self hideView];
|
|
|
+ if (self.callback) {
|
|
|
+ self.callback(SORTACTION_HIDE, @{});
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
|
|
|
+ if ([touch.view isDescendantOfView:self.bgView] || [touch.view isDescendantOfView:self.bottomView]) {
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+/*
|
|
|
+// Only override drawRect: if you perform custom drawing.
|
|
|
+// An empty implementation adversely affects performance during animation.
|
|
|
+- (void)drawRect:(CGRect)rect {
|
|
|
+ // Drawing code
|
|
|
+}
|
|
|
+*/
|
|
|
+
|
|
|
+@end
|