123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // LXViewControllerManager.m
- // qcdzhiosapp
- //
- // Created by lixianglan on 2017/12/19.
- // Copyright © 2017年 张灝. All rights reserved.
- //
- #import "LXViewControllerManager.h"
- #import <JavaScriptCore/JavaScriptCore.h>
- #import "MBProgressHUD.h"
- @interface LXViewControllerManager()
- @property(nonatomic, strong)MBProgressHUD *hud;
- @end
- @implementation LXViewControllerManager
- +(instancetype)shareViewControllerManager{
- static LXViewControllerManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[LXViewControllerManager alloc] init];
- });
- return manager;
- }
- -(void)showActionSheetWithTitle:(NSString *)title
- message:(NSString *)message
- actions:(NSArray *)actions
- {
- if ( !actions || actions.count == 0) {
- return;
- }
- UIAlertController *ctr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
- for (UIAlertAction *action in actions) {
- [ctr addAction:action];
- }
- kMainQueue(^{
- [[[LXViewControllerManager shareViewControllerManager] currentVC] presentViewController:ctr animated:YES completion:nil];
- });
- }
- -(void)showAlertViewWithTitle:(NSString *)title
- message:(NSString *)message
- actions:(NSArray *)actions
- {
- if ( !actions || actions.count == 0) {
- return;
- }
- UIAlertController *ctr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- for (UIAlertAction *action in actions) {
- [ctr addAction:action];
- }
- kMainQueue(^{
- // [[[LXViewControllerManager shareViewControllerManager] currentVC] presentViewController:ctr animated:YES completion:nil];
- [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:ctr animated:YES completion:nil];
- });
- }
- -(void)showAlertViewWithoutActionWithTitle:(NSString *)title
- message:(NSString *)message
- defaultActionTitle:(NSString *)defaultTitle
- {
- [self showAlertViewWithTitle:title message:message actions:@[[UIAlertAction actionWithTitle:defaultTitle style:UIAlertActionStyleCancel handler:nil]]];
- }
- -(void)showAlertViewWithMessage:(NSString *)message{
- [self showAlertViewWithoutActionWithTitle:@"提示" message:message defaultActionTitle:@"确认"];
- }
- -(UIViewController *)currentVC{
- UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
- while (topRootViewController.presentedViewController)
- {
- topRootViewController = topRootViewController.presentedViewController;
- }
-
- return topRootViewController;
- }
- -(void)showHudText:(NSString *)text{
- [self hideHud];
- kMainQueue(^{
- self.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
- self.hud.mode = MBProgressHUDModeIndeterminate;
- self.hud.label.text = text;
- });
- }
- -(void)hideHud{
- kMainQueue(^{
- if (self.hud) {
- [self.hud hideAnimated:YES];
- }
- });
- }
- -(void)showHudOnlyText:(NSString *)text delay:(int)delay{
- if (!text || [text isEqualToString:@""]) {
- return;
- }
- [self hideHud];
- if (delay <= 0) {
- delay = 1;
- }
- kMainQueue(^{
- self.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
- self.hud.mode = MBProgressHUDModeText;
- [self.hud hideAnimated:YES afterDelay:delay];
- self.hud.label.text = text;
- });
- }
- -(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
- if (jsonString == nil) {
- return nil;
- }
-
- NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
- NSError *err;
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
- options:NSJSONReadingMutableContainers
- error:&err];
- if(err) {
- NSLog(@"json解析失败:%@",err);
- return nil;
- }
- return dic;
- }
- - (UIImage *)image:(UIImage*)image size:(CGSize)sizeToFit andCornerRadius:(CGFloat)radius
- {
- CGRect rect = (CGRect){0.f, 0.f, sizeToFit};
-
- UIGraphicsBeginImageContextWithOptions(sizeToFit, NO, UIScreen.mainScreen.scale);
- CGContextAddPath(UIGraphicsGetCurrentContext(),
- [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
- CGContextClip(UIGraphicsGetCurrentContext());
-
- [image drawInRect:rect];
- UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
-
- UIGraphicsEndImageContext();
-
- return output;
- }
- -(NSString *)getJsonStr:(id)object{
- if (!object) {
- return @"";
- }
- NSError* error = nil;
- NSData * result = [NSJSONSerialization dataWithJSONObject:object
- options:kNilOptions error:&error];
- NSString *jsonString = [[NSString alloc] initWithData:result
- encoding:NSUTF8StringEncoding];
- return jsonString;
- }
- @end
|