美文网首页
iOS常用代码块笔记

iOS常用代码块笔记

作者: 数字d | 来源:发表于2021-07-22 11:41 被阅读0次

    1.时间戳和时间工具 yz_time

     NSDate *senddate = [NSDatedate];
            NSLog(@"date1时间戳 = %ld",time(NULL));
            NSString *date2 = [NSStringstringWithFormat:@"%ld", (long)[senddatetimeIntervalSince1970]];
            NSLog(@"date2时间戳 = %@",date2);
            NSDateFormatter *dateformatter = [[NSDateFormatteralloc] init];
            [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            NSString *date1 = [dateformatter stringFromDate:senddate];
            NSLog(@"获取当前时间   = %@",date1);
            // 时间戳转时间
            NSDate *confromTimesp = [NSDatedateWithTimeIntervalSince1970:[senddate timeIntervalSince1970]];
            NSString *confromTimespStr = [dateformatterstringFromDate:confromTimesp];
            NSLog(@"时间戳转时间   = %@",confromTimespStr);
    // 两个时间戳的间隔   返回的是秒
            NSTimeInterval time = [now timeIntervalSinceDate:oldDate];
            NSLog(@"time:%f",time/60/60/24);
    

    2.延时执行工具yz_delay

    // 第一个参数:延迟的时间
    // 可以通过改变队列来改变线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 需要延迟执行的代码
    });
    

    3.给view切一部分圆角yz_corner

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_topSelectView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15,15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _topSelectView.bounds;
    maskLayer.path = maskPath.CGPath;
    _topSelectView.layer.mask = maskLayer;
    

    4.tap点击手势 yz_tap

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editzafNameAction)];
    tap.delegate = self;
    [<#_m1editImg#> addGestureRecognizer:tap];
    

    5单例的实现yz_instance

    #import "<#HPMapLocationService#>.h"
    
    static <#HPMapLocationService#> *instance = nil;
    
    @interface <#HPMapLocationService#>()
    
    @end
    
    @implementation <#HPMapLocationService#>
    
    + (instancetype)sharedInstance
    {
        static dispatch_once_t onceToken;
        
        dispatch_once(&onceToken, ^{
            instance = [[<#HPMapLocationService#> alloc] init];
            
        });
        
        return instance;
    }
    

    6.布尔值的属性创建
    yz_bool

    @property(nonatomic,assign)BOOL  <#selectStatus#>;
    
    

    7.字符串的属性创建

    @property(nonatomic,strong)NSString * <#bankNum#>;
    

    8.cell.m的快速实现yz_cellm

    + (instancetype)cellWithTableView:(UITableView *)tableView
    {
        static NSString *identifier = @"YZFansTasksCell";
        // 1.缓存中取
        YZFansTasksCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        // 2.创建
       if (cell == nil) {
            cell = [[YZFansTasksCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
        
    }
    
    
    /**
     *  构造方法(在初始化对象的时候会调用)
     *  一般在这个方法中添加需要显示的子控件
     */
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
    

    9.img的属性yz_img

    @property(nonatomic,strong)UIImageView * <#zafImg#>;
    

    10.label的属性

    @property(nonatomic,strong)UILabel * <#monthNumLab#>;
    
    

    11.button的懒加载lazybtn

    -(UIButton *)<#editBtn#> {
        if (_<#editBtn#> == nil) {
            _<#editBtn#> = [UIButton buttonWithType:(UIButtonTypeCustom)];
            _<#editBtn#>.frame = CGRectMake(0, 0, 0, 0);
            [_<#editBtn#> addTarget:self action:@selector(editAction) forControlEvents:(UIControlEventTouchUpInside)];
            [_<#editBtn#> setImage:[UIImage imageNamed:@"<#yz_zbing_icon_bj#>"] forState:(UIControlStateNormal)];
        }
        return _<#editBtn#>;
    }
    
    -(void)editAction {
        if (self.block) {
            self.block();
        }
    }
    

    12.label的懒加载lazylab

    -(UILabel *)<#nameLab#> {
        if (_<#nameLab#> == nil) {
            _<#nameLab#> = [[UILabel alloc] init];
            _<#_detailLab#>.text = @"陪练下单 | 礼物打赏 | 兑换商品";
            _<#_detailLab#>.textAlignment = NSTextAlignmentLeft;
            _<#_detailLab#>.font = [UIFont systemFontOfSize:26];
            _<#_detailLab#>.font = [UIFont boldSystemFontOfSize:26];
            _<#_detailLab#>.textColor = UIColorHex(#FFFFFF);
        }
        return _<#nameLab#>;
    }
    

    13.mark提示mark

    #pragma mark --------- <##>
    

    14.方法注释 yz_detail

    /**
     * <#获取直播间的公告信息#>
     */
    

    15.创建一个空方法yz_function

    -(void)<#configInnerUI#> {
        
    }
    

    16.json字符串转字典yz_json

    NSData * data = [<#reason#> dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
    

    17.网络请求yz_request

    //可自行根据每个项目进行补充
    

    18.取消延时执行的代码yz_cancel

    [self performSelector:@selector(<#function#>)
                   withObject:nil
               afterDelay:<#3.0#>];
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                                 selector:@selector(<#function#>)
                                                   object:nil];
    
    -(void) <#function#> {
        
    }
    

    相关文章

      网友评论

          本文标题:iOS常用代码块笔记

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