datePicker 年与日一组 时间一组

2019-08-13  本文已影响0人  高乔人

import <UIKit/UIKit.h>

import "NSDate+Extension.h"

NS_ASSUME_NONNULL_BEGIN

@interface LYXHWDatePickerView : UIView
typedef void(^cancelActioBlock)(void);

@property (nonatomic, retain) NSDate *minLimitDate;//限制最小时间(默认今天)
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
//@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@property (nonatomic, copy) cancelActioBlock cancelBlock;

import "LYXHWDatePickerView.h"

import "UIView+Extension.h"

define kPickerSize self.datePicker.frame.size

define kPickerViewHeight 230

typedef void(^doneBlock)(NSString *starDate,NSString *endDate);
@interface LYXHWDatePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate> {
//日期存储数组
NSMutableArray *_DatesArray;
NSMutableArray *_WeeksArray;
NSMutableArray *_hourArray;
NSMutableArray *_minuteArray;
NSMutableArray *_WeeksTempArray;

//记录位置
NSInteger datesIndex;
NSInteger hourIndex;
NSInteger minuteIndex;
//开始时间、结束时间
NSString *_startDateStr;
NSString *_endDateStr;
NSString *_todayStr;

BOOL _recordEndDate;

}
@property (weak, nonatomic) IBOutlet UIView *buttomView;
@property (weak, nonatomic) IBOutlet UILabel *showYearView;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet UILabel *TipsLabel;

@property (nonatomic, strong) UIPickerView *datePicker;
@property (nonatomic, retain) NSDate *scrollToDate;//滚到指定日期
@property (nonatomic, strong) doneBlock doneBlock;
@end
@implementation LYXHWDatePickerView

-(void)defaultConfig {

if (!_scrollToDate) {
    _scrollToDate = [NSDate date];
}
_startDateStr = @"";
_startDateStr = @"";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM月dd日"];
_todayStr = [dateFormatter stringFromDate:[NSDate date]];

//设置年月日时分数据
_DatesArray = [self setArray:_DatesArray];

// _WeeksArray = [self setArray:_WeeksArray];
// _WeeksTempArray = [self setArray:_WeeksTempArray];
_hourArray = [self setArray:_hourArray];
_minuteArray = [self setArray:_minuteArray];

_WeeksArray = [NSMutableArray arrayWithArray:@[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"]];
_WeeksTempArray = [NSMutableArray arrayWithArray:@[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"]];

for (int i=0; i<60; i++) {
    NSString *num = [NSString stringWithFormat:@"%02d",i];
    if (i<24){
        [_hourArray addObject:num];
    }
    if ([num integerValue] % 5 == 0) {
        [_minuteArray addObject:num];
    }
}
for (NSInteger i=0; i<365; i++) {
    NSString *currentDateStr = @"";

// if (i==0) {
// currentDateStr = @"今天";
// }else{
currentDateStr = [self getLastDate:i];
// }
[_DatesArray addObject:currentDateStr];
}
}

////////


// 得到当前时间(世界标准时间 UTC/GMT)

// NSDate *nowDate = [NSDate date];
// 设置系统时区为本地时区

// 在 GMT 时间基础上追加时间差值,得到本地时间
mydate = [mydate dateByAddingTimeInterval:interval];

NSString *nowDateString = [dateFormatter stringFromDate:mydate];
NSLog(@"%@",nowDateString);
///////////
NSString *week = [self getTheDayOfTheWeekByDateString:nowDateString];
if ([_WeeksTempArray containsObject:week] || [_WeeksArray containsObject:week]) {
    NSInteger index = 0;
    if ([_WeeksTempArray containsObject:week]) {
        index = [_WeeksTempArray indexOfObject:week];
    }else{
        index = [_WeeksArray indexOfObject:week];
    }
    NSString *dateStr = [NSString stringWithFormat:@"%@ %@",currentDateStr,@""];
    
    return dateStr;
}
return @"";

}

pragma mark - UIPickerViewDelegate,UIPickerViewDataSource

// if ([_DatesArray[row] containsObject:@"星"]) {
//
// }
// _DatesArray[row] = [_DatesArray[row] stringByReplacingCharactersInRange:NSMakeRange(12, 3) withString:@""];

if (component==0) {
    if (row < _DatesArray.count) {

// _DatesArray[row] = [_DatesArray[row] stringByReplacingCharactersInRange:NSMakeRange(12, 3) withString:@""];

        title = _DatesArray[row];
    }else{
        title = _DatesArray[row % _DatesArray.count];
    }
}
if (component==1) {
    if (row < _hourArray.count) {
        title = _hourArray[row];
    }else{
        title = _hourArray[row % _hourArray.count];
    }
}
if (component==2) {
    if (row < _minuteArray.count) {
        title = _minuteArray[row];
    }else{
        title = _minuteArray[row % _minuteArray.count];
    }
}

customLabel.text = title;
customLabel.textColor = [UIColor blackColor];
customLabel.adjustsFontSizeToFitWidth = YES;
return customLabel;

}

//滚动到指定的时间位置

///根据时间(dateString)确定当天是星期几,输入的时间格式 yyyy-MM-dd ,如 2015-12-18
-(NSString *)getTheDayOfTheWeekByDateString:(NSString *)dateString{

dateString = [self stringByReplacingsOfString:dateString];
NSString *dateFormat = @"MM-dd";
if(dateString.length > 5){
    dateFormat = @"yyyy-MM-dd";
}
NSDateFormatter *inputFormatter=[[NSDateFormatter alloc]init];

[inputFormatter setDateFormat:dateFormat];

NSDate *formatterDate=[inputFormatter dateFromString:dateString];

NSDateFormatter *outputFormatter=[[NSDateFormatter alloc]init];

[outputFormatter setDateFormat:@"EEEE-MMMM-d"];

NSString *outputDateStr=[outputFormatter stringFromDate:formatterDate];

NSArray *weekArray = [outputDateStr componentsSeparatedByString:@"-"];

return [weekArray objectAtIndex:0];

}

pragma mark - UIGestureRecognizerDelegate

pragma mark - Action

//点击确定

//取消

pragma mark - getter / setter

/*********
比较两个时间差值
*******/

上一篇 下一篇

猜你喜欢

热点阅读