PopWaitAlert.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // PopWaitAlert.m
  3. // PopupViewWrapper
  4. //
  5. // Created by Rui Zhang on 3/20/23.
  6. // Copyright © 2023 yangyi. All rights reserved.
  7. //
  8. #import "PopWaitAlert.h"
  9. #import "RAUtils.h"
  10. @interface PopWaitAlert ()
  11. @property (nonatomic , copy) void (^completion)(void);
  12. //+ (instancetype)sharedInstance;
  13. //(void (^ __nullable)(void)) completion;
  14. @end
  15. @implementation PopWaitAlert
  16. //+ (instancetype)sharedInstance {
  17. //
  18. // static PopWaitAlert *singleton = nil;
  19. // static dispatch_once_t onceToken;
  20. // dispatch_once(&onceToken, ^{
  21. //
  22. // singleton = [[PopWaitAlert alloc] init];
  23. //// [[NSNotificationCenter defaultCenter] addObserver:singleton selector:@selector(handleLogin:) name:User_LoginOK_Notification object:nil];
  24. //
  25. //
  26. //
  27. //// self addObserver:self forKeyPath:self.scan_cart options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
  28. // });
  29. // return singleton;
  30. //}
  31. + (instancetype)Show:(NSString*) title message:(NSString*) message completion:(void (^ __nullable)(void))completion
  32. {
  33. if(title==nil)
  34. title=@"";
  35. if(message==nil)
  36. message=@"";
  37. PopWaitAlert *pa =[[PopWaitAlert alloc] init:completion];
  38. // [pa setCompletion:completion];
  39. [pa setCenter : [RAUtils keyWindow].center ];
  40. [pa setTitle:title];
  41. [pa setMessage:message];
  42. RAPopviewContainer *wrapper = [[RAPopviewContainer alloc] initWithView:pa];
  43. wrapper.modalView=true;
  44. wrapper.bgColor = [UIColor colorWithWhite:0 alpha:0.2];
  45. [wrapper show:[RAUtils keyWindow]];
  46. return pa;
  47. }
  48. -(void)updateframe
  49. {
  50. int h = 0;
  51. h=_labelTitle.frame.size.height+10+_labelMessage.frame.size.height+10+20;
  52. CGRect newframe = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,h);
  53. self.frame = newframe;
  54. }
  55. -(void)setMessage:(NSString*)messageText
  56. {
  57. _labelMessage.text = messageText;
  58. _labelMessage.frame = CGRectMake(16, 45, self.frame.size.width-32, 40);
  59. [_labelMessage sizeToFit];
  60. [self updateframe];
  61. }
  62. -(void)setTitle:(NSString*)titleText
  63. {
  64. _labelTitle.text = titleText;
  65. _labelTitle.frame = CGRectMake(30, 10, self.frame.size.width-60, 40);
  66. [_labelTitle sizeToFit];
  67. [self updateframe];
  68. }
  69. - (void)hide
  70. {
  71. // [UIView animateWithDuration:0.25 animations:^{
  72. // CGRect frame = self.frame;
  73. // frame.origin.y = self.superview.frame.size.height;
  74. // self.frame = frame;
  75. // } completion:^(BOOL finished) {
  76. [super hide];
  77. if(self.completion)
  78. self.completion();
  79. // }];
  80. }
  81. - (instancetype)init:(void (^ __nullable)(void))completion
  82. {
  83. self.completion=completion;
  84. return [self init];
  85. }
  86. - (instancetype)init
  87. {
  88. return [self initWithFrame:CGRectMake(0, 0, 270, 180)];
  89. }
  90. - (instancetype)initWithFrame:(CGRect)frame {
  91. if (self = [super initWithFrame:frame]) {
  92. self.backgroundColor=[UIColor whiteColor];
  93. self.layer.cornerRadius = 15.0f;
  94. self.layer.masksToBounds = YES;
  95. self.layer.borderColor=[UIColor darkGrayColor].CGColor;
  96. self.layer.borderWidth=0.2;
  97. self.layer.shadowRadius = self.layer.cornerRadius + 5;
  98. self.layer.shadowOpacity = 0.1f;
  99. self.layer.shadowOffset = CGSizeMake(0 - (self.layer.cornerRadius+5)/2, 0 - (self.layer.cornerRadius+5)/2);
  100. self.layer.shadowColor = [UIColor blackColor].CGColor;
  101. self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.layer.cornerRadius].CGPath;
  102. // UILabel *clabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-20, 100)];
  103. // clabel.textAlignment = NSTextAlignmentCenter;
  104. // clabel.text = @"Title Title Title Title";
  105. // clabel.font=[UIFont boldSystemFontOfSize:17.0f];
  106. // [clabel setLineBreakMode:NSLineBreakByWordWrapping];
  107. //
  108. // [clabel setNumberOfLines:2];
  109. //
  110. // [clabel sizeToFit];
  111. //
  112. _labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, frame.size.width-60, 40)];
  113. _labelTitle.textAlignment = NSTextAlignmentCenter;
  114. _labelTitle.text = @"Please Wait...";
  115. _labelTitle.font=[UIFont boldSystemFontOfSize:20.0f];
  116. [_labelTitle setLineBreakMode:NSLineBreakByWordWrapping];
  117. [_labelTitle setNumberOfLines:1];
  118. [_labelTitle sizeToFit];
  119. [self addSubview:_labelTitle];
  120. _labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
  121. NSLayoutConstraint *T_hl = [NSLayoutConstraint constraintWithItem:_labelTitle
  122. attribute:NSLayoutAttributeLeading
  123. relatedBy:NSLayoutRelationEqual
  124. toItem:self
  125. attribute:NSLayoutAttributeLeading
  126. multiplier:1
  127. constant:30];
  128. NSLayoutConstraint *T_ht = [NSLayoutConstraint constraintWithItem:_labelTitle
  129. attribute:NSLayoutAttributeTrailing
  130. relatedBy:NSLayoutRelationEqual
  131. toItem:self
  132. attribute:NSLayoutAttributeTrailing
  133. multiplier:1
  134. constant:-30];
  135. NSLayoutConstraint *T_vt = [NSLayoutConstraint constraintWithItem:_labelTitle
  136. attribute:NSLayoutAttributeTop
  137. relatedBy:NSLayoutRelationEqual
  138. toItem:self
  139. attribute:NSLayoutAttributeTop
  140. multiplier:1
  141. constant:10];
  142. // NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:hud
  143. // attribute:NSLayoutAttributeWidth
  144. // relatedBy:NSLayoutRelationEqual
  145. // toItem:nil
  146. // attribute:NSLayoutAttributeNotAnAttribute
  147. // multiplier:0
  148. // constant:HUDSIZE];
  149. // NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:hud
  150. // attribute:NSLayoutAttributeHeight
  151. // relatedBy:NSLayoutRelationEqual
  152. // toItem:nil
  153. // attribute:NSLayoutAttributeNotAnAttribute
  154. // multiplier:0
  155. // constant:HUDSIZE];
  156. [self addConstraints:@[T_hl,T_ht,T_vt]];
  157. // CGSize maxSizeTitle =[labelTitle sizeThatFits:labelTitle.frame.size];
  158. _labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(16, 45, frame.size.width-32, 40)];
  159. _labelMessage.textAlignment = NSTextAlignmentCenter;
  160. _labelMessage.text = @"Message";
  161. _labelMessage.font=[UIFont systemFontOfSize:16.0f];
  162. [_labelMessage setLineBreakMode:NSLineBreakByWordWrapping];
  163. [_labelMessage setNumberOfLines:1];
  164. [_labelMessage sizeToFit];
  165. [self addSubview:_labelMessage];
  166. _labelMessage.translatesAutoresizingMaskIntoConstraints = NO;
  167. NSLayoutConstraint *M_hl = [NSLayoutConstraint constraintWithItem:_labelMessage
  168. attribute:NSLayoutAttributeLeading
  169. relatedBy:NSLayoutRelationEqual
  170. toItem:self
  171. attribute:NSLayoutAttributeLeading
  172. multiplier:1
  173. constant:16];
  174. NSLayoutConstraint *M_ht = [NSLayoutConstraint constraintWithItem:_labelMessage
  175. attribute:NSLayoutAttributeTrailing
  176. relatedBy:NSLayoutRelationEqual
  177. toItem:self
  178. attribute:NSLayoutAttributeTrailing
  179. multiplier:1
  180. constant:-16];
  181. NSLayoutConstraint *M_vt = [NSLayoutConstraint constraintWithItem:_labelMessage
  182. attribute:NSLayoutAttributeTop
  183. relatedBy:NSLayoutRelationEqual
  184. toItem:_labelTitle
  185. attribute:NSLayoutAttributeBottom
  186. multiplier:1
  187. constant:10];
  188. [self addConstraints:@[M_hl,M_ht,M_vt]];
  189. // labelTitle.text = @"Title Title Title Title Title Title Title";
  190. // [labelTitle sizeToFit];
  191. [self updateframe];
  192. // [self sizeToFit];
  193. // self resize
  194. }
  195. return self;
  196. }
  197. /*
  198. // Only override drawRect: if you perform custom drawing.
  199. // An empty implementation adversely affects performance during animation.
  200. - (void)drawRect:(CGRect)rect {
  201. // Drawing code
  202. }
  203. */
  204. @end