LoadingView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // LoadingView.m
  3. // test11113
  4. //
  5. // Created by apple on 15/11/11.
  6. // Copyright (c) 2015年 JCZ. All rights reserved.
  7. //
  8. #import "ActivityView.h"
  9. #import "LoadingView.h"
  10. #import "STButton.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. static LoadingView* myView ;
  13. //
  14. //static STButton* msgBtn;
  15. static NSMutableArray* msgArr;
  16. /**用来播放水滴声音的
  17. */
  18. static AVAudioPlayer* player;
  19. #define HUD_Frame [[UIScreen mainScreen] bounds]
  20. #define HUD_Size HUD_Frame.size
  21. @interface LoadingView()
  22. @property(nonatomic,strong)UILabel* label;
  23. @property(nonatomic,strong)ActivityView* indicator;
  24. @end
  25. @implementation LoadingView
  26. -(id)initWithFrame:(CGRect)frame{
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.backgroundColor = [UIColor colorWithRed:134/255.0 green:204/255.0 blue:71/255.0 alpha:.1];
  30. self.backgroundColor = [UIColor colorWithWhite:.9 alpha:.45];
  31. // self.layer.cornerRadius = 50;
  32. // self.layer.masksToBounds = YES;
  33. _indicator = [[ActivityView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  34. _indicator.center = CGPointMake(50, 50);
  35. [_indicator startAnimating];
  36. [self addSubview:_indicator];
  37. _label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_indicator.frame), _indicator.frame.origin.y, HUD_Size.width, 100)];
  38. _label.textColor = [UIColor orangeColor];
  39. _label.font = [UIFont scaleSize:30];
  40. [self addSubview:_label];
  41. }
  42. return self;
  43. }
  44. +(void)createHUD
  45. {
  46. //如果是后台 就不在展示弹出框了
  47. if (myDelegate.isBackgroundTask) {
  48. return;
  49. }
  50. @try {
  51. if (!myView) {
  52. myView = [[LoadingView alloc] initWithFrame:CGRectMake((kSize.width - 100)/2.0, (kSize.height - 100)/2.0, 100, 100)];
  53. NSString * path = [[NSBundle mainBundle] pathForResource:@"toast.wav" ofType:nil];
  54. NSFileManager* fm = [NSFileManager defaultManager];
  55. if (![fm fileExistsAtPath:path]) {
  56. return;
  57. }
  58. //crash dansonmark 从本地获取音频播放crash 我猜是线程出问题了 这个问题不是必现的
  59. NSURL * url = [NSURL fileURLWithPath:path];
  60. player =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
  61. player.volume = 1;
  62. player.numberOfLoops =0;
  63. }
  64. } @catch (NSException *exception) {
  65. } @finally {
  66. }
  67. }
  68. /**如果真机上遇到MSG无法消除的问题。可以考虑用数组保存label。然后遍历数组移除。
  69. guide_icon7
  70. */
  71. +(void)showMsg:(NSString*)str
  72. {
  73. //如果是后台 就不在展示弹出框了
  74. if (myDelegate.isBackgroundTask) {
  75. return;
  76. }
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. CGFloat w,h;
  79. w = 200;
  80. h = 130;
  81. if (!msgArr) {
  82. msgArr = [NSMutableArray array];
  83. }
  84. for (UIView* v in msgArr) {
  85. [v removeFromSuperview];
  86. }
  87. STButton* msgBtn = [[STButton alloc] initWithFrame:CGRectMake(0,0, w, h)];
  88. // [msgBtn setStyle:2];
  89. msgBtn.center = CGPointMake(kSize.width/2.0, kSize.height/2.0);
  90. msgBtn.backgroundColor = RGB_COLOR(255, 215, 10);
  91. msgBtn.titleLabel.numberOfLines = 0;
  92. [msgBtn setTitle:str textColor:RGB_COLOR(24, 19, 18) font:20 fotState:UIControlStateNormal];
  93. [msgArr addObject:msgBtn];
  94. msgBtn.layer.cornerRadius = 5;
  95. msgBtn.layer.masksToBounds = YES;
  96. msgBtn.backgroundColor = [UIColor colorWithWhite:1 alpha:.8];
  97. [msgBtn setImage:[UIImage imageNamed:@"question_correct.png"] forState:UIControlStateNormal];
  98. [[UIApplication sharedApplication].delegate.window addSubview:msgBtn];
  99. [player play];
  100. [UIView animateWithDuration:2.5 animations:^{
  101. msgBtn.alpha = .7;
  102. }completion:^(BOOL finished) {
  103. [msgBtn removeFromSuperview];
  104. }];
  105. });
  106. }
  107. /**0是普通的警告。1是操作成功的
  108. */
  109. +(void)showMsg:(NSString *)str Style:(int)style
  110. {
  111. //如果是后台 就不在展示弹出框了
  112. if (myDelegate.isBackgroundTask) {
  113. return;
  114. }
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. CGFloat w,h;
  117. w = 200;
  118. h = 130;
  119. if (!msgArr) {
  120. msgArr = [NSMutableArray array];
  121. }
  122. for (UIView* v in msgArr) {
  123. [v removeFromSuperview];
  124. }
  125. STButton* msgBtn = [[STButton alloc] initWithFrame:CGRectMake(0,0, w, h)];
  126. msgBtn.center = CGPointMake(kSize.width/2.0, kSize.height/2.0);
  127. if (0 == style) {
  128. msgBtn.backgroundColor = RGB_COLOR(255, 215, 10);
  129. [msgBtn setImage:[[UIImage imageNamed:@"guide_icon7.png"] tint:RGB_COLOR(24, 19, 18)] forState:UIControlStateNormal];
  130. }else{
  131. msgBtn.backgroundColor = [UIColor colorWithWhite:1 alpha:.8];
  132. [msgBtn setImage:[UIImage imageNamed:@"question_correct.png"] forState:UIControlStateNormal];
  133. }
  134. msgBtn.titleLabel.numberOfLines = 0;
  135. [msgBtn setTitle:str textColor:RGB_COLOR(24, 19, 18) font:20 fotState:UIControlStateNormal];
  136. [msgArr addObject:msgBtn];
  137. msgBtn.layer.cornerRadius = 5;
  138. msgBtn.layer.masksToBounds = YES;
  139. [[UIApplication sharedApplication].keyWindow addSubview:msgBtn];
  140. [player play];
  141. [UIView animateWithDuration:2 animations:^{
  142. msgBtn.alpha = .9;
  143. }completion:^(BOOL finished) {
  144. [msgBtn removeFromSuperview];
  145. }];
  146. });
  147. }
  148. +(void)showHUD{
  149. dispatch_async(dispatch_get_main_queue(), ^{
  150. [LoadingView createHUD];
  151. [myView.indicator startAnimating];
  152. [[UIApplication sharedApplication].keyWindow addSubview:myView];
  153. });
  154. }
  155. +(void)removeHUD
  156. {
  157. dispatch_async(dispatch_get_main_queue(), ^{
  158. //如果都存在 在除掉
  159. if(myView || myView.superview){
  160. [myView.indicator stopAnimating];
  161. [myView removeFromSuperview];
  162. }
  163. });
  164. }
  165. @end
  166. @implementation InputView
  167. -(id)initWithTitle:(NSString*)title{
  168. self = [super initWithFrame:kFrame];
  169. [self setBackgroundColor:[UIColor colorWithWhite:.3 alpha:.6]];
  170. if (self)
  171. {
  172. CGFloat x,y,w,h,bd;
  173. w = 280;
  174. x = (kSize.width - w)/2.0;
  175. y=0;
  176. h = 120;
  177. UIView* cv = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  178. [cv setBackgroundColor:[UIColor whiteColor]];
  179. [self addSubview:cv];
  180. cv.layer.cornerRadius = 10;
  181. cv.layer.masksToBounds = YES;
  182. [cv setClipsToBounds:YES];
  183. contentVi = cv;
  184. UILabel* label;
  185. int num = 4;
  186. int cnt = 10;
  187. bd = 15;
  188. h = (contentVi.width - (num+1)*bd)/num;
  189. x=0;
  190. y=0;
  191. // w=w;
  192. // h=30;
  193. label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
  194. [label setBackgroundColor:defGreen];
  195. [label setTextColor:[UIColor whiteColor]];
  196. [label setText:title];
  197. [label setTextAlignment:NSTextAlignmentCenter];
  198. [cv addSubview:label];
  199. UIButton* btn;
  200. NSInteger tag=0;
  201. bd = 10;
  202. w = h;
  203. x = cv.width - bd - w;
  204. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  205. [btn setTitle:@"×" textColor:[UIColor whiteColor] font:30 fotState:UIControlStateNormal];
  206. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  207. [cv addSubview:btn];
  208. [btn setTag:tag++];
  209. //port 1 -10
  210. num = 4;
  211. cnt = 10;
  212. bd = 15;
  213. w = (contentVi.width - (num+1)*bd)/num;
  214. y+= h + bd;
  215. h = w;
  216. for (int i = 0; i<cnt; i++)
  217. {
  218. x = (bd+w)*(i%num)+bd;
  219. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  220. NSString* path = [NSString stringWithFormat:@"portrait%d.png",i+1];
  221. [btn setImage:[UIImage imageNamed:path] forState:UIControlStateNormal];
  222. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  223. [btn setBackgroundColor:defGreen];
  224. [contentVi addSubview:btn];
  225. btn.layer.cornerRadius = 5;
  226. btn.layer.masksToBounds = YES;
  227. [btn setTag:tag++];
  228. if ( 0 == (i+1)%num)
  229. {
  230. y += h + bd ;
  231. }
  232. }
  233. x = 0;
  234. w = contentVi.width*.5;
  235. if ( 0 != cnt%num)
  236. {
  237. y += h + bd ;
  238. }
  239. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  240. [btn setTitle:@"相册" textColor:defGreen font:20 fotState:UIControlStateNormal];
  241. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  242. [contentVi addSubview:btn];
  243. [btn setTag:tag++];
  244. x += w;
  245. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  246. [btn setTitle:@"拍照" textColor:defGreen font:20 fotState:UIControlStateNormal];
  247. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  248. [contentVi addSubview:btn];
  249. [btn setTag:tag++];
  250. contentVi.height = y+ h;
  251. contentVi.center = self.center;
  252. }
  253. return self;
  254. }
  255. -(void)btnClick:(UIButton*)sender
  256. {
  257. //NSLog(@"btn%d",(int)sender.tag);
  258. if (sender.tag < 1) {
  259. [self cancelAction];
  260. }else if (sender.tag < 11){
  261. NSString* path = [NSString stringWithFormat:@"portrait%d.png",(int)sender.tag];
  262. if (_delegate && [_delegate respondsToSelector:@selector(InputView:didGetImage:)]) {
  263. [_delegate InputView:self didGetImage:[UIImage imageNamed:path]];
  264. [self cancelAction];
  265. }
  266. return;
  267. }
  268. if ( 11 == sender.tag){
  269. self.type = 0;
  270. if (_delegate && [_delegate respondsToSelector:@selector(InputViewWillPickImage:)]) {
  271. [_delegate InputViewWillPickImage:self];
  272. [self removeFromSuperview];
  273. }
  274. return;
  275. }
  276. if ( 12 == sender.tag){
  277. self.type = 1;
  278. if (_delegate && [_delegate respondsToSelector:@selector(InputViewWillPickImage:)]) {
  279. [_delegate InputViewWillPickImage:self];
  280. [self removeFromSuperview];
  281. }
  282. return;
  283. }
  284. }
  285. -(void)show
  286. {
  287. [[UIApplication sharedApplication].keyWindow addSubview:self];
  288. contentVi.transform = CGAffineTransformScale(contentVi.transform, .3, .3);
  289. self.alpha = .1;
  290. [UIView animateWithDuration:.5 animations:^{
  291. self.alpha = 1;
  292. contentVi.transform = CGAffineTransformIdentity;
  293. } ];
  294. }
  295. -(void)cancelAction
  296. {
  297. self.userInteractionEnabled = NO;
  298. [UIView animateWithDuration:.5 animations:^{
  299. self.alpha = .1;
  300. contentVi.transform = CGAffineTransformScale(contentVi.transform, .3, .3);
  301. } completion:^(BOOL finished) {
  302. [self removeFromSuperview];
  303. }];
  304. }
  305. @end
  306. #pragma mark -
  307. void ShowHUD()
  308. {
  309. [LoadingView showHUD];
  310. }
  311. void showHUDTitle(NSString* title)
  312. {
  313. ShowHUD();
  314. [myView.label setText:title];
  315. }
  316. void RemoveHUD()
  317. {
  318. [LoadingView removeHUD];
  319. }
  320. void ShowMsg(NSString* str)
  321. {
  322. [LoadingView showMsg:str];
  323. }
  324. void ShowMsgEmpty(){
  325. [LoadingView showMsg:@"暂无信息"];
  326. }
  327. void showMsgUnconnect(){
  328. [LoadingView showMsg:@"网络未连接"];
  329. RemoveHUD();
  330. }
  331. void showMsgLoginPls(){
  332. [LoadingView showMsg:@"请登录后操作"];
  333. }
  334. void ShowMsgUnOpen()
  335. {
  336. [LoadingView showMsg:@"暂未开放,敬请期待"];
  337. }
  338. void ShowMsgFailed()
  339. {
  340. [LoadingView showMsg:@"操作失败"];
  341. }
  342. void ShowMsgSuc()
  343. {
  344. [LoadingView showMsg:@"操作成功" Style:1];
  345. }