iOS AILinkBleSDK - 蓝牙SDK
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ELInputAlertView.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // ELInputAlertView.m
  3. // 自定义tabbar
  4. //
  5. // Created by iot_user on 2019/1/19.
  6. // Copyright © 2019年 IOT. All rights reserved.
  7. //
  8. #import "ELInputAlertView.h"
  9. #import "CABasicAnimation+ELAnimation.h"
  10. #import "UIView+Category.h"
  11. #import "Masonry.h"
  12. //字符串是否为空
  13. //#define IsEmptyStr(str) [NSString isEmptyString:str]
  14. //375x667的屏幕
  15. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  16. #define ScreenWeight [UIScreen mainScreen].bounds.size.width
  17. //
  18. //#define SW(w) ScreenWeight*(w/375.0)
  19. //#define SH(h) ScreenHeight*(h/667.0)
  20. #define AlertWidth 285
  21. #define AlertHeight 285
  22. #define ButtonHeight 45
  23. #define ButtonEdge 25
  24. @interface ELInputAlertView()
  25. @property (nonatomic, strong) UIView * alertView;
  26. @property (nonatomic, strong) UITextField * inputTF;
  27. @property (nonatomic, strong) UIButton *topBtn;
  28. @property (nonatomic, strong) UIButton *bottomBtn;
  29. @end
  30. @implementation ELInputAlertView
  31. -(UITextField *)inputTF{
  32. if (_inputTF == nil) {
  33. _inputTF = [[UITextField alloc]init];
  34. _inputTF.textColor = [UIColor blackColor];
  35. _inputTF.font = [UIFont systemFontOfSize:18];
  36. _inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  37. // _inputTF.layer.borderColor = EL242GrayColor.CGColor;
  38. // _inputTF.layer.borderWidth = 1.0f;
  39. //
  40. UIView *leftView = [[UIView alloc]init];
  41. leftView.frame = CGRectMake(0, 0, 5, 5);
  42. _inputTF.leftView = leftView;
  43. _inputTF.leftViewMode = UITextFieldViewModeAlways;
  44. }
  45. return _inputTF;
  46. }
  47. -(instancetype)initWithTittle:(NSString *)title withText:(NSString *)text withMessage:(NSString *)placeholder withLeftButton:(NSString *)leftTitle withRightButton:(NSString *)rightTitle{
  48. if (self = [super init]) {
  49. self.frame = [UIScreen mainScreen].bounds;
  50. self.leftAutoHide = YES;
  51. self.rightAutoHide = NO;
  52. //
  53. UIView * backgrand = [[UIView alloc]init];
  54. backgrand.backgroundColor = [UIColor blackColor];
  55. backgrand.alpha = 0.5;
  56. backgrand.frame = self.frame;
  57. [self addSubview:backgrand];
  58. //
  59. UIView * alertView = [[UIView alloc]init];
  60. self.alertView = alertView;
  61. alertView.frame = CGRectMake((ScreenWeight-AlertWidth)/2, (ScreenHeight - AlertHeight)/2, AlertWidth, AlertHeight);
  62. alertView.backgroundColor = [UIColor whiteColor];
  63. [self addSubview:alertView];
  64. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:alertView.bounds byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5, 5)];
  65. CAShapeLayer * layer = [CAShapeLayer layer];
  66. layer.frame = alertView.bounds;
  67. layer.path = path.CGPath;
  68. alertView.layer.mask = layer;
  69. //标题
  70. UILabel * titleLbl = [[UILabel alloc]init];
  71. titleLbl.text = title;
  72. titleLbl.textColor = [UIColor blackColor];
  73. titleLbl.font = [UIFont systemFontOfSize:16];
  74. titleLbl.textAlignment = NSTextAlignmentCenter;
  75. titleLbl.frame = CGRectMake(10, 10, alertView.frame.size.width-20, 25);
  76. [alertView addSubview:titleLbl];
  77. //输入框
  78. // if (IsEmptyStr(placeholder)) {
  79. // placeholder = @"";
  80. // }
  81. self.inputTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:placeholder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
  82. self.inputTF.text = text;
  83. [self.inputTF becomeFirstResponder];
  84. self.inputTF.frame = CGRectMake(10, CGRectGetMaxY(titleLbl.frame)+55, alertView.frame.size.width-20, 40);
  85. [alertView addSubview:self.inputTF];
  86. //
  87. UIView *lineView = [[UIView alloc] init];
  88. lineView.backgroundColor = [UIColor blackColor];
  89. [alertView addSubview:lineView];
  90. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.mas_equalTo(15);
  92. make.right.mas_equalTo(-15);
  93. make.top.equalTo(self.inputTF.mas_bottom).mas_offset(5);
  94. make.height.mas_equalTo(1);
  95. }];
  96. //下边按钮
  97. UIButton * leftButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
  98. self.bottomBtn = leftButton;
  99. [leftButton setTitle:leftTitle forState:(UIControlStateNormal)];
  100. [leftButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
  101. [leftButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
  102. leftButton.titleLabel.font = [UIFont systemFontOfSize:18];
  103. leftButton.frame = CGRectMake(ButtonEdge, alertView.frame.size.height-ButtonHeight-5, alertView.frame.size.width-ButtonEdge*2, ButtonHeight);
  104. [alertView addSubview:leftButton];
  105. [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:(UIControlEventTouchUpInside)];
  106. //上边按钮
  107. UIButton * rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
  108. self.topBtn = rightButton;
  109. [rightButton setTitle:rightTitle forState:(UIControlStateNormal)];
  110. [rightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  111. [rightButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
  112. rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
  113. rightButton.frame = CGRectMake(ButtonEdge, alertView.frame.size.height-ButtonHeight*2-10, alertView.frame.size.width-ButtonEdge*2, ButtonHeight);
  114. [alertView addSubview:rightButton];
  115. rightButton.backgroundColor = [UIColor blueColor];
  116. [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:(UIControlEventTouchUpInside)];
  117. [rightButton setCornerRadii:CGSizeMake(ButtonHeight/2, ButtonHeight/2) withCorner:(UIRectCornerAllCorners)];
  118. // //竖线
  119. // UIView *sxLine = [[UIView alloc]init];
  120. // sxLine.frame = CGRectMake(alertView.frame.size.width/2, alertView.frame.size.height-ButtonHeight, 1, ButtonHeight);
  121. // sxLine.backgroundColor = EL242GrayColor;
  122. // [alertView addSubview:sxLine];
  123. // //横线
  124. // UIView * hxLine = [[UIView alloc]init];
  125. // hxLine.backgroundColor = EL242GrayColor;
  126. // hxLine.frame = CGRectMake(0, alertView.frame.size.height-ButtonHeight, alertView.frame.size.width, 1);
  127. // [alertView addSubview:hxLine];
  128. //
  129. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  130. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  131. }
  132. return self;
  133. }
  134. -(void)setTopBtnBackColor:(UIColor *)topBtnBackColor{
  135. _topBtnBackColor = topBtnBackColor;
  136. self.topBtn.backgroundColor = topBtnBackColor;
  137. }
  138. -(void)keyboardWillShow:(NSNotification *)noti{
  139. CGRect frame = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  140. CGRect alertFrame = self.alertView.frame;
  141. alertFrame.origin.y = ScreenHeight - frame.size.height - AlertHeight - 10>0?ScreenHeight - frame.size.height - AlertHeight - 10:0;
  142. self.alertView.frame = alertFrame;
  143. }
  144. -(void)keyboardWillHide:(NSNotification *)noti{
  145. CGRect alertFrame = self.alertView.frame;
  146. alertFrame.origin.y = (ScreenHeight - AlertHeight)/2;
  147. self.alertView.frame = alertFrame;
  148. }
  149. -(void)leftAction:(UIButton *)sender{
  150. if (self.leftBlock) {
  151. self.leftBlock(self.inputTF.text);
  152. [self hide];
  153. [self.inputTF resignFirstResponder];
  154. }
  155. if (self.leftHideBlock) {
  156. BOOL hide = self.leftHideBlock(self.inputTF.text, self);
  157. if (hide == YES) {
  158. [self hide];
  159. [self.inputTF resignFirstResponder];
  160. }
  161. }
  162. if (self.leftAutoHide) {
  163. [self hide];
  164. [self.inputTF resignFirstResponder];
  165. }
  166. }
  167. -(void)rightAction:(UIButton *)sender{
  168. if (self.rightBlock) {
  169. self.rightBlock(self.inputTF.text);
  170. [self hide];
  171. [self.inputTF resignFirstResponder];
  172. }
  173. if (self.rightHideBlock) {
  174. BOOL hide = self.rightHideBlock(self.inputTF.text, self);
  175. if (hide == YES) {
  176. [self.inputTF resignFirstResponder];
  177. [self hide];
  178. }
  179. }
  180. if (self.rightAutoHide) {
  181. [self.inputTF resignFirstResponder];
  182. [self hide];
  183. }
  184. }
  185. -(void)show{
  186. UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
  187. [window addSubview:self];
  188. [self.alertView.layer addAnimation:[CABasicAnimation scaleShowAnimationWithDuration:ELAnimationDuration] forKey:@"ELInputAlertViewShowAnimation"];
  189. }
  190. -(void)hide{
  191. [self.alertView.layer addAnimation:[CABasicAnimation scaleHideAnimationWithDuration:ELAnimationDuration] forKey:@"ELInputAlertViewHideAnimation"];
  192. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ELAnimationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  193. [self removeFromSuperview];
  194. });
  195. }
  196. @end