GCD創(chuàng)建定時(shí)器

@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);
// 取消定時(shí)器
            dispatch_cancel(self.timer);
            self.timer = nil;

GCD定時(shí)器使用小注意

dispatch_resume(self.timer); 開啟GCD的定時(shí)器 
 如果你在這句代碼之后 在寫這么一句dispatch_resume(self.timer);  崩    
你只能暫停 也就是這句代碼dispatch_suspend(self.timer); 暫停之后 你只能開啟 也就是這個(gè) dispatch_resume(self.timer); 
你如果在暫停情況下取消 也就是這個(gè) dispatch_cancel(self.timer); 崩
可以在開啟情況下取消

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

柵欄函數(shù)

 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;
    });

七等分 簡(jiǎn)單 直截了當(dāng)?shù)膶懛?/h1>

巧用倍數(shù)約束 和 移動(dòng)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等分更簡(jiǎ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)

時(shí)間格式化

- (NSString *)getTimeStrWithString:(NSString *)str
{//2018-04-18 18:42:00.0
    //str = @"2018-03-14 19:46";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:SS.0"]; //設(shè)定時(shí)間的格式

    NSDate *tempDate = [dateFormatter dateFromString:str];//將字符串轉(zhuǎn)換為時(shí)間對(duì)象
    
    NSDateFormatter * dateF = [[NSDateFormatter alloc]init];
    [dateF setDateFormat:@"MM-dd HH:mm"];
    NSString * timeStr = [dateF stringFromDate:tempDate];
    return timeStr;
}

wkwebView執(zhí)行js的彈窗

#if 0
#pragma mark --- 彈窗
設(shè)置代理
_webView.UIDelegate = self;
 _webView.navigationDelegate = self;
執(zhí)行代理方法
- (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與當(dāng)前控制器的強(qiáng)引用問題

//JS調(diào)用OC 添加處理腳本

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


在向JS中注入handler的時(shí)候強(qiáng)引用了self,最終導(dǎo)致內(nèi)存泄漏
userCC addScriptMessageHandler:(nonnull id<WKScriptMessageHandler>) name:(nonnull NSString *)

//解決方案
666

窗口一旦創(chuàng)建 不能一刻無根控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    if ([LDUserTool isAutoLogin]) {
*  重點(diǎn)(*@ο@*) 哇~************************************
        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;
}

時(shí)間格式化

-(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小應(yīng)用

// 頁面開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD showWithStatus:@"加載中"];
    
}
// 頁面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [SVProgressHUD dismiss];
}

秒 ->時(shí)分秒

//傳入 秒  得到 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簡(jiǎn)單使用

- (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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市壹将,隨后出現(xiàn)的幾起案子禽最,更是在濱河造成了極大的恐慌腺怯,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件川无,死亡現(xiàn)場(chǎng)離奇詭異呛占,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)懦趋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門晾虑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人仅叫,你說我怎么就攤上這事帜篇。” “怎么了诫咱?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵笙隙,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我坎缭,道長(zhǎng)竟痰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任掏呼,我火速辦了婚禮坏快,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哄尔。我一直安慰自己假消,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布岭接。 她就那樣靜靜地躺著富拗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鸣戴。 梳的紋絲不亂的頭發(fā)上啃沪,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音窄锅,去河邊找鬼创千。 笑死缰雇,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的追驴。 我是一名探鬼主播械哟,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼殿雪!你這毒婦竟也來了暇咆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤丙曙,失蹤者是張志新(化名)和其女友劉穎爸业,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亏镰,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扯旷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了索抓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钧忽。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖纸兔,靈堂內(nèi)的尸體忽然破棺而出惰瓜,到底是詐尸還是另有隱情,我是刑警寧澤汉矿,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布崎坊,位于F島的核電站,受9級(jí)特大地震影響洲拇,放射性物質(zhì)發(fā)生泄漏奈揍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一赋续、第九天 我趴在偏房一處隱蔽的房頂上張望男翰。 院中可真熱鬧,春花似錦纽乱、人聲如沸蛾绎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽租冠。三九已至,卻和暖如春薯嗤,著一層夾襖步出監(jiān)牢的瞬間顽爹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來泰國打工骆姐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镜粤,地道東北人捏题。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像肉渴,于是被迫代替她去往敵國和親公荧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容