iOS AILinkBleSDK - 蓝牙SDK
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ELSelectView.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // ELSelectView.m
  3. // Elink
  4. //
  5. // Created by iot_user on 2019/1/23.
  6. // Copyright © 2019年 iot_iMac. All rights reserved.
  7. //
  8. #import "ELSelectView.h"
  9. #import "CABasicAnimation+ELAnimation.h"
  10. #import "UILabel+WZAdd.h"
  11. #import "Masonry.h"
  12. #import "UIButton+WZAdd.h"
  13. #import "AppDelegate.h"
  14. //375x667的屏幕
  15. #define ScreenH [UIScreen mainScreen].bounds.size.height
  16. #define ScreenW [UIScreen mainScreen].bounds.size.width
  17. #define MaxHeight ScreenH/2
  18. #define MaxWeight 250
  19. #define CellHeight 45
  20. @implementation ELSelectViewCell
  21. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  22. if (self = [super initWithStyle:style
  23. reuseIdentifier:reuseIdentifier]) {
  24. self.titleLbl = [UILabel createWithFrame:CGRectZero bgColor:[UIColor whiteColor] text:nil textColor:[UIColor grayColor] font:[UIFont systemFontOfSize:18] align:NSTextAlignmentCenter];
  25. self.titleLbl.numberOfLines=0;
  26. self.titleLbl.adjustsFontSizeToFitWidth = YES;
  27. [self.contentView addSubview:self.titleLbl];
  28. [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.top.left.right.bottom.mas_equalTo(0);
  30. }];
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. }
  33. return self;
  34. }
  35. @end
  36. static NSString * ELSelectViewCellid = @"ELSelectViewCellid";
  37. @interface ELSelectView()<UITableViewDelegate, UITableViewDataSource>
  38. @property (nonatomic, strong) UITableView * tableView;
  39. @property (nonatomic, strong) UIView *alertView;
  40. @property (nonatomic, copy) NSArray * datas;
  41. @end
  42. @implementation ELSelectView
  43. -(UITableView *)tableView{
  44. if (_tableView == nil) {
  45. _tableView = [[UITableView alloc]init];
  46. _tableView.delegate = self;
  47. _tableView.dataSource = self;
  48. _tableView.tableFooterView = [UIView new];
  49. _tableView.separatorColor = [UIColor clearColor];
  50. }
  51. return _tableView;
  52. }
  53. -(instancetype)initWithTitle:(NSString *)title withSelectArray:(NSArray<NSString *> *)array{
  54. if (self = [super init]) {
  55. self.frame = [UIScreen mainScreen].bounds;
  56. self.datas = [array copy];
  57. //
  58. UIView * backgrand = [[UIView alloc]init];
  59. backgrand.backgroundColor = [UIColor blackColor];
  60. backgrand.alpha = 0.5;
  61. backgrand.frame = self.frame;
  62. [self addSubview:backgrand];
  63. //
  64. CGFloat height = (array.count+1)*CellHeight;
  65. height = (height > MaxHeight?MaxHeight:height)+33;
  66. //TopMargin
  67. UIView * alertView = [[UIView alloc]init];
  68. self.alertView = alertView;
  69. alertView.frame = CGRectMake((ScreenW-MaxWeight)/2,(ScreenH-height)/2, MaxWeight, height);
  70. alertView.backgroundColor = [UIColor whiteColor];
  71. // alertView.layer.cornerRadius = 5;
  72. [self addSubview:alertView];
  73. //
  74. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:alertView.bounds byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5, 5)];
  75. CAShapeLayer * layer = [CAShapeLayer layer];
  76. layer.frame = alertView.bounds;
  77. layer.path = path.CGPath;
  78. alertView.layer.mask = layer;
  79. //
  80. UILabel * titleLbl = [UILabel createWithFrame:CGRectZero bgColor:[UIColor whiteColor] text:title textColor:[UIColor lightGrayColor] font:[UIFont systemFontOfSize:15] align:NSTextAlignmentCenter];
  81. [alertView addSubview:titleLbl];
  82. [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.left.right.mas_equalTo(0);
  84. make.height.mas_equalTo(33);
  85. }];
  86. //
  87. UIButton * cancelBtn = [UIButton createWithFrame:CGRectZero bgColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16] norTitle:@"取消" norTitleColor:[UIColor lightGrayColor] norImage:nil borderColor:nil needRoundCorner:NO target:self action:@selector(cancelAction:)];
  88. [cancelBtn setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
  89. [alertView addSubview:cancelBtn];
  90. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.bottom.left.right.mas_equalTo(0);
  92. make.height.mas_equalTo(CellHeight);
  93. }];
  94. //
  95. [alertView addSubview:self.tableView];
  96. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.left.right.mas_equalTo(0);
  98. make.top.equalTo(titleLbl.mas_bottom).mas_offset(0);
  99. make.bottom.equalTo(cancelBtn.mas_top).mas_equalTo(0);
  100. }];
  101. }
  102. return self;
  103. }
  104. -(void)cancelAction:(UIButton *)sender{
  105. [self hide];
  106. }
  107. -(void)show{
  108. UIWindow * window = [UIApplication sharedApplication].windows.firstObject;;
  109. [window addSubview:self];
  110. [self.alertView.layer addAnimation:[CABasicAnimation scaleShowAnimationWithDuration:ELAnimationDuration] forKey:@"ELSelectViewShowAnimation"];
  111. }
  112. -(void)hide{
  113. [self.alertView.layer addAnimation:[CABasicAnimation scaleHideAnimationWithDuration:ELAnimationDuration] forKey:@"ELSelectViewHideAnimation"];
  114. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ELAnimationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  115. [self removeFromSuperview];
  116. });
  117. }
  118. #pragma mark ============ UITableView数据源方法 ==============
  119. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  120. return 1;
  121. }
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  123. return self.datas.count;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. ELSelectViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ELSelectViewCellid];
  127. if (cell == nil) {
  128. cell = [[ELSelectViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ELSelectViewCellid];
  129. }
  130. cell.titleLbl.text = self.datas[indexPath.row];
  131. return cell;
  132. }
  133. #pragma mark ============ UITableView代理 ==============
  134. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  135. ELSelectViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
  136. cell.titleLbl.textColor = [UIColor blueColor];
  137. cell.titleLbl.font = [UIFont systemFontOfSize:20];
  138. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ELAnimationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  139. if (self.selectRowBlock) {
  140. self.selectRowBlock(indexPath.row);
  141. }
  142. [self hide];
  143. });
  144. }
  145. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  146. return CellHeight;
  147. }
  148. @end