GCD创建定时器

@property(nonatomic,strong)dispatch_source_t timer;
dispatch_queue_t queue = dispatch_get_main_queue();
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@".....");
    });
    dispatch_resume(self.timer);
// 取消定时器
            dispatch_cancel(self.timer);
            self.timer = nil;

GCD定时器使用小注意

dispatch_resume(self.timer); 开启GCD的定时器 
 如果你在这句代码之后 在写这么一句dispatch_resume(self.timer);  崩    
你只能暂停 也就是这句代码dispatch_suspend(self.timer); 暂停之后 你只能开启 也就是这个 dispatch_resume(self.timer); 
你如果在暂停情况下取消 也就是这个 dispatch_cancel(self.timer); 崩
可以在开启情况下取消

我开启定时器了 然后在控制器pop方法里 dispatch_suspend(self.timer);暂停掉 定时器 
在dealloc里if (self.timer) {
        dispatch_cancel(self.timer);
        self.timer = nil;
        //nil = nil;
    }
崩 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

栅栏函数

 dispatch_queue_t queue = dispatch_queue_create("LDD", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"1️⃣____%d",i);
        }
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"...栅栏1️⃣...");
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"2️⃣____%d",i);
        }
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"...栅栏2️⃣...");
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"3️⃣____%d",i);
        }
    });

三等分

 UILabel * label1 = ({
        UILabel * label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor redColor];
        [self.view addSubview:label];
        label.text = @"左边的Label";
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view.mas_centerY);
            make.left.mas_equalTo(0);
            
        }];
        label;
    });
    UILabel * label2 = ({
        UILabel * label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor greenColor];
        [self.view addSubview:label];
        label.text = @"中间的Label";
       
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view.mas_centerY);
            make.left.equalTo(label1.mas_right).offset(0);
            make.width.equalTo(label1.mas_width);
        }];
        label;
    });
    UILabel * label3 = ({
        UILabel * label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor blueColor];
        [self.view addSubview:label];
        label.text = @"右边的Label";
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view.mas_centerY);
            make.left.equalTo(label2.mas_right).offset(0);
            make.right.mas_equalTo(-0);
            make.width.equalTo(label2.mas_width);
            
        }];
        label;
    });

七等分 简单 直截了当的写法

巧用倍数约束 和 移动lastLabel

UILabel *lastLabel;
    for (NSUInteger i = 0; i < 7; i++){
        UILabel *weekLb = [[UILabel alloc] init];
        weekLb.text = _weekDayArray[i];
        weekLb.textColor = LS_COLORS_TXT_GRAY_DARK;
        weekLb.font = [UIFont systemFontOfSize:14];
        weekLb.textAlignment = NSTextAlignmentCenter;
        [_weekView addSubview:weekLb];
        if(i==0){
            [weekLb mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(_collectionView.mas_width).multipliedBy(0.142);//1 / 7
                make.left.equalTo(_weekView.mas_left);
                make.centerY.equalTo(_weekView);
            }];
        }else{
            [weekLb mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(_collectionView.mas_width).multipliedBy(0.142);
                make.left.equalTo(lastLabel.mas_right);
                make.centerY.equalTo(_weekView);
            }];
        }
        lastLabel = weekLb;
    }
[source.netPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]

N等分更简洁明了的写法

UIStackView * stack = ({
        UIStackView * view = [[UIStackView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 60)];
        view.axis = UILayoutConstraintAxisHorizontal;
        view.distribution = UIStackViewDistributionFillEqually;
        view.spacing = 10;
        view.alignment = UIStackViewAlignmentFill;
        for (int i = 0; i < 4; i ++) {
            UIView * subView = [[UIView alloc]init];
            subView.backgroundColor = [UIColor colorWithRed:random()%256/255.0 green:random()%256/255.0 blue:random()%256/255.0 alpha:1];
            [view addArrangedSubview:subView];
        }
        view;
    });
    [self.view addSubview:stack];

三等分之VFL

let v1 = UIView()
v1.backgroundColor = UIColor.red
let v2 = UIView()
v2.backgroundColor = UIColor.green
let v3 = UIView()
v3.backgroundColor = UIColor.blue
v1.translatesAutoresizingMaskIntoConstraints = false
v2.translatesAutoresizingMaskIntoConstraints = false
v3.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(v1)
view.addSubview(v2)
view.addSubview(v3)
let views = ["v1":v1,"v2":v2,"v3":v3]
let cons =  NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[v1]-0-[v2(==v1)]-0-[v3(==v1)]-0-|", options: [.alignAllTop,.alignAllBottom], metrics: nil, views: views)
view.addConstraints(cons)
let vv = NSLayoutConstraint.constraints(withVisualFormat: "V:[v1(50)]-20-|", options: [], metrics: nil, views: views)
view.addConstraints(vv)

时间格式化

- (NSString *)getTimeStrWithString:(NSString *)str
{//2018-04-18 18:42:00.0
    //str = @"2018-03-14 19:46";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:SS.0"]; //设定时间的格式

    NSDate *tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
    
    NSDateFormatter * dateF = [[NSDateFormatter alloc]init];
    [dateF setDateFormat:@"MM-dd HH:mm"];
    NSString * timeStr = [dateF stringFromDate:tempDate];
    return timeStr;
}

wkwebView执行js的弹窗

#if 0
#pragma mark --- 弹窗
设置代理
_webView.UIDelegate = self;
 _webView.navigationDelegate = self;
执行代理方法
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler
{
    
}
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler
{
    
}
#endif

wkwebview与当前控制器的强引用问题

//JS调用OC 添加处理脚本

WKUserContentController *userCC = config.userContentController;//提供使用 JavaScript post 信息和注射 script 的方法。WKWebViewConfiguration
 [userCC addScriptMessageHandler:self name:@"showName"];


在向JS中注入handler的时候强引用了self,最终导致内存泄漏
userCC addScriptMessageHandler:(nonnull id<WKScriptMessageHandler>) name:(nonnull NSString *)

//解决方案
666

窗口一旦创建 不能一刻无根控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    if ([LDUserTool isAutoLogin]) {
*  重点(*@ο@*) 哇~************************************
        UIViewController *emptyView = [[UIViewController alloc] init];
        self.window.rootViewController = emptyView;
******************************************************
        [self saveSessionid:^{
             self.window.rootViewController = [[MtTabBarController alloc]init];
        } fail:^{
             self.window.rootViewController = [[MtTabBarController alloc]init];
        }];
          
    }else{
        self.window.rootViewController = [[PlatformSelectController alloc]init];
    }
    [self.window makeKeyAndVisible];
    [self monitorNetworkStatus];
    return YES;
}

时间格式化

-(NSString *)getMMSSFromSS:(NSString *)totalTime{
    
    NSInteger seconds = [totalTime integerValue];
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    return format_time;
}

Loading小应用

// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD showWithStatus:@"加载中"];
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [SVProgressHUD dismiss];
}

秒 ->时分秒

//传入 秒  得到 xx:xx:xx
-(NSString *)getMMSSFromSS:(NSString *)totalTime{

    NSInteger seconds = [totalTime integerValue];

    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];

    return format_time;

}

秒 -> 分秒

//传入 秒  得到  xx分钟xx秒
-(NSString *)getMMSSFromSS:(NSString *)totalTime{

    NSInteger seconds = [totalTime integerValue];

    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];

    NSLog(@"format_time : %@",format_time);

    return format_time;

}

HUD简单使用

- (void)showMessage:(NSString*)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.contentColor = [UIColor blackColor];
    hud.label.text = message;
    hud.label.textColor = [UIColor whiteColor];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    [hud hideAnimated:YES afterDelay:1.0];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341

推荐阅读更多精彩内容