SelectBoxView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // SelectBoxView.m
  3. // WebSocketDemo
  4. //
  5. // Created by 刘云鸽 on 2019/2/27.
  6. // Copyright © 2019年 liuyunge. All rights reserved.
  7. //
  8. #import "SelectBoxView.h"
  9. #import <Masonry.h>
  10. #define WEAKSELF __weak typeof(self) weakSelf = self;
  11. static CGFloat const animationTime = 0.3;
  12. static CGFloat const rowheight = 42;
  13. @interface SelectBoxView()<UITableViewDelegate,UITableViewDataSource>
  14. @end
  15. @implementation SelectBoxView
  16. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  17. if (self = [super initWithCoder:aDecoder]) {
  18. [self setUI];
  19. }
  20. return self;
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. if (self = [super initWithFrame:frame]) {
  24. [self setUI];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame dataSource:(NSMutableArray *)dataSource {
  29. if (self = [super initWithFrame:frame]) {
  30. self.dataSource = dataSource;
  31. [self setUI];
  32. }
  33. return self;
  34. }
  35. - (void)setUI {
  36. self.cornerRadius = 5;
  37. self.borderWidth = 1;
  38. self.borderColor = [UIColor colorWithRed:153.0/255 green:153.0/255 blue:153.0/255 alpha:1];
  39. [self addSubview:self.rightImageView];
  40. [self addSubview:self.titleLabel];
  41. [self addSubview:self.maskBtn];
  42. [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.centerY.equalTo(self);
  44. make.right.equalTo(self.mas_right).offset(-10);
  45. }];
  46. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.centerY.equalTo(self);
  48. make.left.equalTo(self.mas_left).offset(10);
  49. make.right.equalTo(self.rightImageView);
  50. }];
  51. [self.maskBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.left.right.top.bottom.equalTo(self);
  53. }];
  54. }
  55. - (void)show {
  56. WEAKSELF;
  57. UIWindow * window = [UIApplication sharedApplication].keyWindow;
  58. [window addSubview:self.backgroundBtn];
  59. [window addSubview:self.tableView];
  60. // 获取按钮在屏幕中的位置
  61. CGRect frame = [self convertRect:self.bounds toView:window];
  62. CGFloat tableViewY = frame.origin.y + frame.size.height;
  63. CGRect tableViewFrame;
  64. tableViewFrame.size.width = frame.size.width;
  65. tableViewFrame.size.height = self.tableViewHeight;
  66. tableViewFrame.origin.x = frame.origin.x;
  67. if (tableViewY + self.tableViewHeight < CGRectGetHeight([UIScreen mainScreen].bounds)) {
  68. tableViewFrame.origin.y = tableViewY;
  69. self.isDirectionUp = NO;
  70. }else {
  71. tableViewFrame.origin.y = frame.origin.y - self.tableViewHeight;
  72. self.isDirectionUp = YES;
  73. }
  74. self.tableView.frame = CGRectMake(tableViewFrame.origin.x, tableViewFrame.origin.y+(self.isDirectionUp?self.tableViewHeight:0), tableViewFrame.size.width, 0);
  75. [UIView animateWithDuration:animationTime animations:^{
  76. weakSelf.rightImageView.transform = CGAffineTransformRotate(weakSelf.rightImageView.transform,self.isDirectionUp?-M_PI:M_PI);
  77. weakSelf.tableView.frame = CGRectMake(tableViewFrame.origin.x, tableViewFrame.origin.y, tableViewFrame.size.width, tableViewFrame.size.height);
  78. NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
  79. }];
  80. }
  81. - (void)dismiss {
  82. WEAKSELF;
  83. [UIView animateWithDuration:animationTime animations:^{
  84. weakSelf.rightImageView.transform = CGAffineTransformIdentity;
  85. 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);
  86. } completion:^(BOOL finished) {
  87. [weakSelf.backgroundBtn removeFromSuperview];
  88. [weakSelf.tableView removeFromSuperview];
  89. }];
  90. }
  91. #pragma mark UITableViewDelegate,UITableViewDataSource
  92. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  93. return self.dataSource.count;
  94. }
  95. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
  97. cell.textLabel.text = self.dataSource[indexPath.row];
  98. return cell;
  99. }
  100. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  101. self.title = self.dataSource[indexPath.row];
  102. [self dismiss];
  103. if ([self.delegate respondsToSelector:@selector(selectBoxView:selectedIndex:)]) {
  104. [self.delegate selectBoxView:self selectedIndex:indexPath.row];
  105. }
  106. if (self.selectedBlock) {
  107. self.selectedBlock(self, indexPath.row);
  108. }
  109. }
  110. #pragma mark getter && setter
  111. - (UILabel *)titleLabel {
  112. if (!_titleLabel) {
  113. _titleLabel = [UILabel new];
  114. _titleLabel.text = @"请选择场所";
  115. _titleLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1];
  116. _titleLabel.font = [UIFont systemFontOfSize:16];
  117. }
  118. return _titleLabel;
  119. }
  120. - (UIImageView *)rightImageView {
  121. if(!_rightImageView) {
  122. _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hsh_login_drop-down"]];
  123. _rightImageView.clipsToBounds = YES;
  124. }
  125. return _rightImageView;
  126. }
  127. - (UIButton *)maskBtn {
  128. if (!_maskBtn) {
  129. _maskBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  130. _maskBtn.backgroundColor = [UIColor clearColor];
  131. _maskBtn.clipsToBounds = YES;
  132. [_maskBtn addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
  133. }
  134. return _maskBtn;
  135. }
  136. - (UITableView *)tableView {
  137. if (!_tableView) {
  138. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  139. _tableView.tableFooterView = [UIView new];
  140. _tableView.rowHeight = rowheight;
  141. _tableView.delegate = self;
  142. _tableView.dataSource = self;
  143. _tableView.backgroundColor = [UIColor whiteColor];
  144. _tableView.layer.shadowOffset = CGSizeMake(4, 4);
  145. _tableView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
  146. _tableView.layer.shadowOpacity = 0.8;
  147. _tableView.layer.shadowRadius = 4;
  148. _tableView.layer.borderColor = [UIColor grayColor].CGColor;
  149. _tableView.layer.borderWidth = 0.5;
  150. _tableView.layer.cornerRadius = self.cornerRadius;
  151. _tableView.separatorInset = UIEdgeInsetsMake(0, 5, 0, 5);
  152. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
  153. if (@available(iOS 11.0, *)) {
  154. _tableView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
  155. }
  156. }
  157. return _tableView;
  158. }
  159. - (UIButton *)backgroundBtn {
  160. if (!_backgroundBtn) {
  161. _backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  162. _backgroundBtn.backgroundColor = [UIColor clearColor];
  163. _backgroundBtn.frame = [UIScreen mainScreen].bounds;
  164. [_backgroundBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  165. }
  166. return _backgroundBtn;
  167. }
  168. - (void)setRowHeight:(CGFloat)rowHeigt {
  169. _rowHeight = rowHeigt;
  170. self.tableView.rowHeight = rowHeigt;
  171. }
  172. - (void)setTitle:(NSString *)title {
  173. _title = title;
  174. self.titleLabel.text = title;
  175. }
  176. - (void)setDataSource:(NSMutableArray *)dataSource {
  177. _dataSource = dataSource;
  178. if (self.rowHeight) {
  179. self.tableViewHeight = self.dataSource.count*self.rowHeight;
  180. }else {
  181. self.tableViewHeight = self.dataSource.count*rowheight;
  182. }
  183. }
  184. - (void)setTitleFontSize:(CGFloat)titleFontSize {
  185. _titleFontSize = titleFontSize;
  186. self.titleLabel.font = [UIFont systemFontOfSize:titleFontSize];
  187. }
  188. - (void)setTitleColor:(UIColor *)titleColor {
  189. _titleColor = titleColor;
  190. self.titleLabel.textColor = titleColor;
  191. }
  192. - (void)setCornerRadius:(CGFloat)cornerRadius {
  193. self.layer.cornerRadius = cornerRadius;
  194. }
  195. - (CGFloat)cornerRadius {
  196. return self.layer.cornerRadius;
  197. }
  198. - (void)setBorderColor:(UIColor *)borderColor {
  199. self.layer.borderColor = borderColor.CGColor;
  200. }
  201. - (UIColor *)borderColor {
  202. return (UIColor *)self.layer.borderColor;
  203. }
  204. - (void)setBorderWidth:(CGFloat)borderWidth {
  205. self.layer.borderWidth = borderWidth;
  206. }
  207. - (CGFloat)borderWidth {
  208. return self.layer.borderWidth;
  209. }
  210. /*
  211. // Only override drawRect: if you perform custom drawing.
  212. // An empty implementation adversely affects performance during animation.
  213. - (void)drawRect:(CGRect)rect {
  214. // Drawing code
  215. }
  216. */
  217. @end