@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];
}