LXViewControllerManager.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // LXViewControllerManager.m
  3. // qcdzhiosapp
  4. //
  5. // Created by lixianglan on 2017/12/19.
  6. // Copyright © 2017年 张灝. All rights reserved.
  7. //
  8. #import "LXViewControllerManager.h"
  9. #import <JavaScriptCore/JavaScriptCore.h>
  10. #import "MBProgressHUD.h"
  11. @interface LXViewControllerManager()
  12. @property(nonatomic, strong)MBProgressHUD *hud;
  13. @end
  14. @implementation LXViewControllerManager
  15. +(instancetype)shareViewControllerManager{
  16. static LXViewControllerManager *manager = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. manager = [[LXViewControllerManager alloc] init];
  20. });
  21. return manager;
  22. }
  23. -(void)showActionSheetWithTitle:(NSString *)title
  24. message:(NSString *)message
  25. actions:(NSArray *)actions
  26. {
  27. if ( !actions || actions.count == 0) {
  28. return;
  29. }
  30. UIAlertController *ctr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
  31. for (UIAlertAction *action in actions) {
  32. [ctr addAction:action];
  33. }
  34. kMainQueue(^{
  35. [[[LXViewControllerManager shareViewControllerManager] currentVC] presentViewController:ctr animated:YES completion:nil];
  36. });
  37. }
  38. -(void)showAlertViewWithTitle:(NSString *)title
  39. message:(NSString *)message
  40. actions:(NSArray *)actions
  41. {
  42. if ( !actions || actions.count == 0) {
  43. return;
  44. }
  45. UIAlertController *ctr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  46. for (UIAlertAction *action in actions) {
  47. [ctr addAction:action];
  48. }
  49. kMainQueue(^{
  50. // [[[LXViewControllerManager shareViewControllerManager] currentVC] presentViewController:ctr animated:YES completion:nil];
  51. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:ctr animated:YES completion:nil];
  52. });
  53. }
  54. -(void)showAlertViewWithoutActionWithTitle:(NSString *)title
  55. message:(NSString *)message
  56. defaultActionTitle:(NSString *)defaultTitle
  57. {
  58. [self showAlertViewWithTitle:title message:message actions:@[[UIAlertAction actionWithTitle:defaultTitle style:UIAlertActionStyleCancel handler:nil]]];
  59. }
  60. -(void)showAlertViewWithMessage:(NSString *)message{
  61. [self showAlertViewWithoutActionWithTitle:@"提示" message:message defaultActionTitle:@"确认"];
  62. }
  63. -(UIViewController *)currentVC{
  64. UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  65. while (topRootViewController.presentedViewController)
  66. {
  67. topRootViewController = topRootViewController.presentedViewController;
  68. }
  69. return topRootViewController;
  70. }
  71. -(void)showHudText:(NSString *)text{
  72. [self hideHud];
  73. kMainQueue(^{
  74. self.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
  75. self.hud.mode = MBProgressHUDModeIndeterminate;
  76. self.hud.label.text = text;
  77. });
  78. }
  79. -(void)hideHud{
  80. kMainQueue(^{
  81. if (self.hud) {
  82. [self.hud hideAnimated:YES];
  83. }
  84. });
  85. }
  86. -(void)showHudOnlyText:(NSString *)text delay:(int)delay{
  87. if (!text || [text isEqualToString:@""]) {
  88. return;
  89. }
  90. [self hideHud];
  91. if (delay <= 0) {
  92. delay = 1;
  93. }
  94. kMainQueue(^{
  95. self.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
  96. self.hud.mode = MBProgressHUDModeText;
  97. [self.hud hideAnimated:YES afterDelay:delay];
  98. self.hud.label.text = text;
  99. });
  100. }
  101. -(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
  102. if (jsonString == nil) {
  103. return nil;
  104. }
  105. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  106. NSError *err;
  107. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  108. options:NSJSONReadingMutableContainers
  109. error:&err];
  110. if(err) {
  111. NSLog(@"json解析失败:%@",err);
  112. return nil;
  113. }
  114. return dic;
  115. }
  116. - (UIImage *)image:(UIImage*)image size:(CGSize)sizeToFit andCornerRadius:(CGFloat)radius
  117. {
  118. CGRect rect = (CGRect){0.f, 0.f, sizeToFit};
  119. UIGraphicsBeginImageContextWithOptions(sizeToFit, NO, UIScreen.mainScreen.scale);
  120. CGContextAddPath(UIGraphicsGetCurrentContext(),
  121. [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
  122. CGContextClip(UIGraphicsGetCurrentContext());
  123. [image drawInRect:rect];
  124. UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
  125. UIGraphicsEndImageContext();
  126. return output;
  127. }
  128. -(NSString *)getJsonStr:(id)object{
  129. if (!object) {
  130. return @"";
  131. }
  132. NSError* error = nil;
  133. NSData * result = [NSJSONSerialization dataWithJSONObject:object
  134. options:kNilOptions error:&error];
  135. NSString *jsonString = [[NSString alloc] initWithData:result
  136. encoding:NSUTF8StringEncoding];
  137. return jsonString;
  138. }
  139. @end