新手ios项目总结(四)

1.在使用SDWebImage的时候网上有很多使用方法解释,我就不细写了,我们编程经常忽略的就是在报内存警告的时候在内存图片如何处理:

 1 在AppDelegate导入#import "SDWebImageManager.h"
 2 -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
 3     
 4     //取消下载
 5     SDWebImageManager *SDManager=[SDWebImageManager sharedManager];
 6     
 7     [SDManager cancelAll];
 8     
 9     //清除内存中所有图片
10     [SDManager.imageCache clearMemory];
11 }

 2.在网络中请求的json数据,在这些数据转模型的时候经常是使用我们常用的转模型方法,这样是在数据多的情况下使用常用方法就参数很多的重复代码,提供一种集成库MJExtension  他的文档在   https://github.com/CoderMJLee/MJExtension/

3.在程序退出到后台的时候,所有的动画,定时器,多媒提。联网操作很难完成,如果想要在后台运行一些定时器,需要告诉后天我还有些任务要处理

/**
 *  当app进入后台的时候调用
 *
 *  @param application <#application description#>
 */

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    
    UIBackgroundTaskIdentifier task=[application beginBackgroundTaskWithExpirationHandler:^{
        
        //当后台任务到时间结束的时候就会调用这个block
        //赶紧结束这个任务
        [application endBackgroundTask:task];
        
    }];
    
    //在info.plist中设置后天模式:Required background modes
    
    //搞一个0kb的mp3文件没有声音的   循环播放
    
}


 3.//这个是在父控件中的子控件排序

-(void)layoutSubviews 

     1、init初始化不会触发layoutSubviews

   但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会触发

     2、addSubview会触发layoutSubviews

    3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化

    4、滚动一个UIScrollView会触发layoutSubviews

    5、旋转Screen会触发父UIView上的layoutSubviews事件

    6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

 layoutSubviews, 当我们在某个类的内部调整子视图位置时,需要调用。反过来的意思就是说:如果你想要在外部设置subviews的位置,就不要重写。

4.0.在控件类中重写setFrame方法的目的;拦截设置按钮的尺寸的过程,,用想在系统计算机和设置完按钮的的尺寸后,再修改一下尺寸 注意修改的代码必须写在 [super setFrame:frame];

5.0.自定义cell时候,在cell的contentView添加各种View各种子控件,在点击这些cell的时候就会发现,出现cell的内容会被cell的背景色所遮盖,第二就是点击cell是他的高亮颜色遮盖cell的内容。解决方法是选择的时候下列两个方法在选择和高亮的时候内容view的背景色不变

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.originView.backgroundColor=[UIColor whiteColor];
    

    // Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    
    [super setHighlighted:highlighted animated:animated];
     self.originView.backgroundColor = [UIColor whiteColor];
}

或者直接设置cell选中的模式:selectionStyle属性:UITableViewCellSelectionStyleNone

6.0如何自定义cell的颜色 cell其中的一个属性selectedBackgroundView

7.0如果有真机调试,转换欧美时间需要设置locale

 dataFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];

8.0比较两个日期需要知道差多少分钟多少秒多少天多少月使用日历NSCalendar对象

 //建立一个日历对象
    NSCalendar *calendar=[NSCalendar currentCalendar];
    //需要确定比较的对象
    NSCalendarUnit calendarUnit=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond;
    //比较方法
    NSDateComponents *DateComponents=[calendar components:calendarUnit fromDate:weiBoContend toDate:now options:0];

这就是各个时间比较的结果

9.0如何确定时间是昨天今天,今年去年

 NSDateFormatter *dataFormatter=[[NSDateFormatter alloc]init];
    
    dataFormatter.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";
    
    //在真机的时候需要当地的
    dataFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    NSDate *weiBoContend=[dataFormatter dateFromString:_created_at];
    
    NSDate *now=[NSDate date];
    
    //建立一个日历对象
    NSCalendar *calendar=[NSCalendar currentCalendar];
    //需要确定比较的对象
    NSCalendarUnit calendarUnit=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond;
    
    NSDateComponents *components=[calendar components:calendarUnit fromDate:weiBoContend toDate:now options:0];
    //比较方法,我需要做个功能就是处理微博的时间刚刚和几分钟前,几小时钱 昨天
   
   
    if([weiBoContend judgeYearWhetherSame]){

        if ([weiBoContend judgeDayWhetherSame]) {
            
            dataFormatter.dateFormat=@"昨天 HH:mm";
            
            NSString *yesterdaytTime=[dataFormatter stringFromDate:weiBoContend];
            
            return yesterdaytTime;
            
            
        }else if([weiBoContend judgeDayWhetherNewDay]){
            
            if (components.hour>1) {
                
                return [NSString stringWithFormat:@"%ld小时之前",components.hour];
                
                
            }if(components.minute>1){
                
                return  [NSString stringWithFormat:@"%ld分钟之前",components.minute];
            }else{
                
                return  [NSString stringWithFormat:@"刚刚"];
            }
            
   
        }else{
            
            dataFormatter.dateFormat=@"MM-dd HH:mm:ss";
            
            NSString *pastTimesDay=[dataFormatter stringFromDate:weiBoContend];
            
            return pastTimesDay;

        }
        
        
    }else{
        
        dataFormatter.dateFormat=@"yyyy-MM-dd HH:mm";
        NSString *newDateYear =[dataFormatter stringFromDate:weiBoContend];
        return newDateYear;
    
        
    }
   
    return _created_at;
}
//增加本时间和现在的时间进行比较,对出时间的进度(昨天,今天,今年和去年)
/**
 *  //如何判断年的方法,
 *  data为微博的时间
 *  @param BOOL 返回是否是同一年
 *
 *  @return
 */


-(BOOL)judgeYearWhetherSame{
    
    NSCalendar *calendar=[NSCalendar currentCalendar];
    //获取当前data的年月日
    NSInteger weiboYear=[calendar component:NSCalendarUnitYear fromDate:self];
    
    NSInteger newYear=[calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return weiboYear==newYear;
    
}
/**
 *  比较微博的时间和现在的时间是不是相差大于一天
 *
 *  @param date 微博的时间
 *
 *  @return 是否是大于1天
 */
-(BOOL)judgeDayWhetherSame{
    
    NSDate *newDate=[NSDate date];
    
    NSDateFormatter *dateFrame=[[NSDateFormatter alloc]init];
    
    dateFrame.dateFormat=@"yyyy:MM:dd";
    
    //返回微博没有时间的格式 现在和过去
    NSString *WeiboStringDate=[dateFrame stringFromDate:self];
    NSString *newDateString=[dateFrame stringFromDate:newDate];
    //这类别中是不能直接给self赋值的,应为是self调用,如果要赋值的话,建立一个参数传入self,,修改参数
    NSDate *currentTime=[[NSDate alloc]init];
    currentTime=self;
    
    NSDate *date=[dateFrame dateFromString:WeiboStringDate];
    newDate=[dateFrame dateFromString:newDateString];
    
    //NSLog(@"weibo%@,new%@",date,newDate);
    NSCalendar *calendar=[NSCalendar currentCalendar];
    
    NSCalendarUnit calendarUnit=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
    
    NSDateComponents *components=[calendar components:calendarUnit fromDate:date toDate:newDate options:0];
    
    
    return components.year==0 && components.month==0 && components.day==1;
    
}
/**
 *  这个判断是否是今天和昨天和其他的时间区分开
 *
 *  @param date 微博内容的时间
 *
 *  @return
 */
-(BOOL)judgeDayWhetherNewDay{
    
    NSDate *newDate=[NSDate date];
    
    NSCalendar *calendar=[NSCalendar currentCalendar];
    
    NSCalendarUnit calendarUnit=NSCalendarUnitDay;
    
    NSDateComponents *components=[calendar components:calendarUnit fromDate:self toDate:newDate options:0];
    
    return  components.day==0;
    
}
原文地址:https://www.cnblogs.com/xiangruru/p/6494596.html