iOS AILinkBleSDK - 蓝牙SDK
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ELSelectView.m 6.6KB

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