// // SelectBoxView.m // WebSocketDemo // // Created by 刘云鸽 on 2019/2/27. // Copyright © 2019年 liuyunge. All rights reserved. // #import "SelectBoxView.h" #import #define WEAKSELF __weak typeof(self) weakSelf = self; static CGFloat const animationTime = 0.3; static CGFloat const rowheight = 42; @interface SelectBoxView() @end @implementation SelectBoxView - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self setUI]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setUI]; } return self; } - (instancetype)initWithFrame:(CGRect)frame dataSource:(NSMutableArray *)dataSource { if (self = [super initWithFrame:frame]) { self.dataSource = dataSource; [self setUI]; } return self; } - (void)setUI { self.cornerRadius = 5; self.borderWidth = 1; self.borderColor = [UIColor colorWithRed:153.0/255 green:153.0/255 blue:153.0/255 alpha:1]; [self addSubview:self.rightImageView]; [self addSubview:self.titleLabel]; [self addSubview:self.maskBtn]; [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self); make.right.equalTo(self.mas_right).offset(-10); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self); make.left.equalTo(self.mas_left).offset(10); make.right.equalTo(self.rightImageView); }]; [self.maskBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.bottom.equalTo(self); }]; } - (void)show { WEAKSELF; UIWindow * window = [UIApplication sharedApplication].keyWindow; [window addSubview:self.backgroundBtn]; [window addSubview:self.tableView]; // 获取按钮在屏幕中的位置 CGRect frame = [self convertRect:self.bounds toView:window]; CGFloat tableViewY = frame.origin.y + frame.size.height; CGRect tableViewFrame; tableViewFrame.size.width = frame.size.width; tableViewFrame.size.height = self.tableViewHeight; tableViewFrame.origin.x = frame.origin.x; if (tableViewY + self.tableViewHeight < CGRectGetHeight([UIScreen mainScreen].bounds)) { tableViewFrame.origin.y = tableViewY; self.isDirectionUp = NO; }else { tableViewFrame.origin.y = frame.origin.y - self.tableViewHeight; self.isDirectionUp = YES; } self.tableView.frame = CGRectMake(tableViewFrame.origin.x, tableViewFrame.origin.y+(self.isDirectionUp?self.tableViewHeight:0), tableViewFrame.size.width, 0); [UIView animateWithDuration:animationTime animations:^{ weakSelf.rightImageView.transform = CGAffineTransformRotate(weakSelf.rightImageView.transform,self.isDirectionUp?-M_PI:M_PI); weakSelf.tableView.frame = CGRectMake(tableViewFrame.origin.x, tableViewFrame.origin.y, tableViewFrame.size.width, tableViewFrame.size.height); NSLog(@"%@",NSStringFromCGRect(self.tableView.frame)); }]; } - (void)dismiss { WEAKSELF; [UIView animateWithDuration:animationTime animations:^{ weakSelf.rightImageView.transform = CGAffineTransformIdentity; weakSelf.tableView.frame = CGRectMake(weakSelf.tableView.frame.origin.x, weakSelf.tableView.frame.origin.y+(self.isDirectionUp?self.tableViewHeight:0), weakSelf.tableView.frame.size.width, 0); } completion:^(BOOL finished) { [weakSelf.backgroundBtn removeFromSuperview]; [weakSelf.tableView removeFromSuperview]; }]; } #pragma mark UITableViewDelegate,UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])]; cell.textLabel.text = self.dataSource[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.title = self.dataSource[indexPath.row]; [self dismiss]; if ([self.delegate respondsToSelector:@selector(selectBoxView:selectedIndex:)]) { [self.delegate selectBoxView:self selectedIndex:indexPath.row]; } if (self.selectedBlock) { self.selectedBlock(self, indexPath.row); } } #pragma mark getter && setter - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.text = @"请选择场所"; _titleLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1]; _titleLabel.font = [UIFont systemFontOfSize:16]; } return _titleLabel; } - (UIImageView *)rightImageView { if(!_rightImageView) { _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hsh_login_drop-down"]]; _rightImageView.clipsToBounds = YES; } return _rightImageView; } - (UIButton *)maskBtn { if (!_maskBtn) { _maskBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _maskBtn.backgroundColor = [UIColor clearColor]; _maskBtn.clipsToBounds = YES; [_maskBtn addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside]; } return _maskBtn; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.tableFooterView = [UIView new]; _tableView.rowHeight = rowheight; _tableView.delegate = self; _tableView.dataSource = self; _tableView.backgroundColor = [UIColor whiteColor]; _tableView.layer.shadowOffset = CGSizeMake(4, 4); _tableView.layer.shadowColor = [UIColor lightGrayColor].CGColor; _tableView.layer.shadowOpacity = 0.8; _tableView.layer.shadowRadius = 4; _tableView.layer.borderColor = [UIColor grayColor].CGColor; _tableView.layer.borderWidth = 0.5; _tableView.layer.cornerRadius = self.cornerRadius; _tableView.separatorInset = UIEdgeInsetsMake(0, 5, 0, 5); [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever; } } return _tableView; } - (UIButton *)backgroundBtn { if (!_backgroundBtn) { _backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _backgroundBtn.backgroundColor = [UIColor clearColor]; _backgroundBtn.frame = [UIScreen mainScreen].bounds; [_backgroundBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; } return _backgroundBtn; } - (void)setRowHeight:(CGFloat)rowHeigt { _rowHeight = rowHeigt; self.tableView.rowHeight = rowHeigt; } - (void)setTitle:(NSString *)title { _title = title; self.titleLabel.text = title; } - (void)setDataSource:(NSMutableArray *)dataSource { _dataSource = dataSource; if (self.rowHeight) { self.tableViewHeight = self.dataSource.count*self.rowHeight; }else { self.tableViewHeight = self.dataSource.count*rowheight; } } - (void)setTitleFontSize:(CGFloat)titleFontSize { _titleFontSize = titleFontSize; self.titleLabel.font = [UIFont systemFontOfSize:titleFontSize]; } - (void)setTitleColor:(UIColor *)titleColor { _titleColor = titleColor; self.titleLabel.textColor = titleColor; } - (void)setCornerRadius:(CGFloat)cornerRadius { self.layer.cornerRadius = cornerRadius; } - (CGFloat)cornerRadius { return self.layer.cornerRadius; } - (void)setBorderColor:(UIColor *)borderColor { self.layer.borderColor = borderColor.CGColor; } - (UIColor *)borderColor { return (UIColor *)self.layer.borderColor; } - (void)setBorderWidth:(CGFloat)borderWidth { self.layer.borderWidth = borderWidth; } - (CGFloat)borderWidth { return self.layer.borderWidth; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end