| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // SelectBoxView.m
- // WebSocketDemo
- //
- // Created by 刘云鸽 on 2019/2/27.
- // Copyright © 2019年 liuyunge. All rights reserved.
- //
- #import "SelectBoxView.h"
- #import <Masonry.h>
- #define WEAKSELF __weak typeof(self) weakSelf = self;
- static CGFloat const animationTime = 0.3;
- static CGFloat const rowheight = 42;
- @interface SelectBoxView()<UITableViewDelegate,UITableViewDataSource>
- @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
|