美文网首页
GCD创建定时器

GCD创建定时器

作者: LD_左岸 | 来源:发表于2017-12-15 11:39 被阅读39次
@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]]

时间格式化

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

相关文章

  • tableView或者scrollView滑动时 定时器 不响应

    先写解决办法 1. 2. 使用GCD创建定时器。GCD创建定时器不收Runloop的影响,并且GCD的定时器更精准...

  • GCD中的定时器(五)

    一. GCD中定时器的使用方法 创建一个队列 创建一个GCD的定时器 设置定时器的开始时间,间隔时间以及精准度 设...

  • GCD定时器

    创建GCD定时器 注意:默认创建的定时器对象需要强引用保住它的命,在任务执行中不被释放 定时器操作函数 dispa...

  • GCD定时器的实现

    GCD定时器 GCD定时器不受RunLoop约束,比NSTimer更加准时 证明,实现GCD定时器

  • GCD进入后台继续运行

    iOS GCD创建标准定时器,进入后台后任可继续定时

  • iOS - GCD中的定时器

    GCD定时器优点:① GCD定时器不受RunLoop约束② 比NSTimer更加准时。 GCD定时器用法敲 dis...

  • GCD定时器

    GCD定时器 GCD定时器不受RunLoop的影响,调用频率比较固定

  • GCD定时器使用

    iOS中的常用定时器分为这几类: NSTimer CADisplayLink GCD定时器 选择GCD定时器原因:...

  • GCD创建定时器

    GCD定时器使用小注意 栅栏函数 三等分 七等分 简单 直截了当的写法 巧用倍数约束 和 移动lastLabel ...

  • 创建GCD定时器

    举个栗子:

网友评论

      本文标题:GCD创建定时器

      本文链接:https://www.haomeiwen.com/subject/rzwrwxtx.html