iOS - 日历设置,支持同时多选设置不同类型的日期

2016-11-08  本文已影响211人  Mr_Bob_
前言:

由于公司是做的医疗服务这块的,所以提出了工作室预约,设置日期为,工作日,半休日,休息日三种,还要支持多选功能,下面提供下具体的做法,需要的小伙伴可以拿去哈
具体的效果如下:

Untitled01.gif
具体用法:

由于封装的方法比较多,我就先把使用方法写出来,改变下套路.

 //  为了防止有重影,最好是先移除
    [self.calendarView removeFromSuperview];
    self.calendarView = nil;
    
    NSDateFormatter *Mformatter = [[NSDateFormatter alloc] init];
    Mformatter.dateFormat = @"yyyy-MM";
    NSString *monthStr = [Mformatter stringFromDate:self.date];
    
    // 网络请求,设置工作室已经设置的日期
    NSMutableDictionary *listPrams = [NSMutableDictionary dictionary];
    listPrams[@"month"] = monthStr;
    listPrams[@"studioid"] = _studio_id;
    __weak typeof(self) weakself = self;
    [WSService requestBusiness:URL_BOOKING_WORKDAY_LISTBYMONTH params:listPrams sucCallBack:^(id result) {
        BOOL isValid = [APIRequestHandler isValidDataWithResult:result];
        if (isValid) {
            NSArray *data = result[@"data"];
            // 三种不同的日期类型
            NSMutableArray *restArray = [NSMutableArray array];
            NSMutableArray *mendArray = [NSMutableArray array];
            NSMutableArray *workArray = [NSMutableArray array];
            
            for (int i = 0; i < data.count; i ++) {
                NSDictionary *dict = data[i];
                NSString *caltype = dict[@"caltype"];
                NSString *calander = dict[@"calander"];
                calander = [calander substringWithRange:NSMakeRange(calander.length - 2,2)];
                if ([caltype isEqualToString:CALTYPE_RESTDAY]) {  // 休息日
                    [restArray addObject:calander];
                }else if ([caltype isEqualToString:CALTYPE_MENDDAY]) {  // 半休日
                    [mendArray addObject:calander];
                }else if ([caltype isEqualToString:CALTYPE_WORKDAY]) {  // 工作日
                    [workArray addObject:calander];
                }
            }
            
            weakself.calendarView = [[FyCalendarView alloc] initWithFrame:CGRectMake(0, 64, PDScreeenW, PDScreeenW)];
            
            weakself.calendarView.allDaysArr = workArray;
            weakself.calendarView.allDaysColor = workColor;
            
            weakself.calendarView.partDaysArr = mendArray;
            weakself.calendarView.partDaysColor = mendColor;
            
            weakself.calendarView.otherDaysArr = restArray;
            weakself.calendarView.otherDaysColor = restColor;
            
            weakself.calendarView.date = [NSDate date];
            weakself.calendarView.dateColor = selcetColor;
            
            [weakself.view addSubview:weakself.calendarView];
            
            // 默认选中当前日期
            UIButton *defaultButton = weakself.calendarView.defaultBtn;
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd";
            NSString *dateStr = [formatter stringFromDate:weakself.date];
            if (defaultButton.selected) {
                [weakself.buttonsSelet addObject:defaultButton];
                [weakself.datesArray addObject:dateStr];
            }
            
            __weak typeof(weakself) weak_self = weakself;
            weakself.calendarView.calendarBlock =  ^(NSString *selectDate, UIButton *selectBtn){
                if (selectBtn.selected) {
                    [weak_self.buttonsSelet addObject:selectBtn];
                    [weak_self.datesArray addObject:selectDate];
                }else{
                    [weak_self.buttonsSelet removeObject:selectBtn];
                    [weak_self.datesArray removeObject:selectDate];
                }
                
            };
            // 设置下个月
            weakself.calendarView.nextMonthBlock = ^(){
                [weak_self setupNextMonth];
                
            };
            // last month
            weakself.calendarView.lastMonthBlock = ^(){
                [weak_self setupLastMonth];
            };
            
        }
    } failCallBack:^(id error) {
        
    } hasHud:NO];

接下来的这两个方法自己实现下,一样的道理

[weak_self setupNextMonth];
[weak_self setupLastMonth];
具体实现代码:
#import <UIKit/UIKit.h>

@protocol CalendarView <NSObject>
/**
 *  点击返回的日期
 */
- (void)setupToday:(NSInteger)day Month:(NSInteger)month Year:(NSInteger)year;
@end

@interface CalendarView : UIView

//set 选择日期
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) UIColor *dateColor;
//年月label
@property (nonatomic, strong) UILabel *headlabel;
// 选中文字颜色
@property (nonatomic, strong) UIColor *headColor;
//weekView
@property (nonatomic, strong) UIView *weekBg;
@property (nonatomic, strong) UIColor *weekDaysColor;

// 全天可用
@property (nonatomic, strong) NSMutableArray *allDaysArr;
@property (nonatomic, strong) UIColor *allDaysColor;

// 部分时段可用
@property (nonatomic, strong) NSMutableArray *partDaysArr;
@property (nonatomic, strong) UIColor *partDaysColor;

// 外加部分选择
@property (nonatomic, strong) NSMutableArray *otherDaysArr;
@property (nonatomic, strong) UIColor *otherDaysColor;

// 是否只显示本月日期,默认->NO
@property (nonatomic, assign) BOOL isShowOnlyMonthDays;

// 默认当前日期的按钮
@property (weak, nonatomic) UIButton *defaultBtn;

//创建日历
- (void)createCalendarViewWith:(NSDate *)date;
/**
 *  nextMonth
 *
 *  @param date nil = 当前日期的下一个月
 */
//- (void)setNextMonth:(NSDate *)date;
- (NSDate *)nextMonth:(NSDate *)date;

/**
 *  lastMonth
 *
 *  @param date nil -> 当前日期的上一个月
 */
//- (void)setLastMonth:(NSDate *)date;
- (NSDate *)lastMonth:(NSDate *)date;

/**
 *  nextMonth and lastMonth
 */
@property (nonatomic, copy) void(^nextMonthBlock)();
@property (nonatomic, copy) void(^lastMonthBlock)();
/**
 *  点击返回日期
 */
@property (nonatomic, copy) void(^calendarBlock)(NSString *seletDate, UIButton *selectBtn);
#import "CalendarView.h"
#import "UIColor+Hex.h"

@interface CalendarView ()
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray *daysArray;

@end

@implementation CalendarView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDate];
        [self setupNextAndLastMonthView];
    }
    return self;
}

- (void)setupDate {
    self.daysArray = [NSMutableArray arrayWithCapacity:42];
    for (int i = 0; i < 42; i++) {
        UIButton *button = [[UIButton alloc] init];
        [self addSubview:button];
        [_daysArray addObject:button];
        [button addTarget:self action:@selector(logDate:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)setupNextAndLastMonthView {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"to_left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(nextAndLastMonth:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    leftBtn.tag = 1;
    leftBtn.frame = CGRectMake(0, 0, 40, 40);
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"to_right"] forState:UIControlStateNormal];
    rightBtn.tag = 2;
    [rightBtn addTarget:self action:@selector(nextAndLastMonth:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    rightBtn.frame = CGRectMake(self.frame.size.width - 40, 0, 40, 40);
}

- (void)nextAndLastMonth:(UIButton *)button {
    if (button.tag == 1) {
        if (self.lastMonthBlock) {
            self.lastMonthBlock();
        }
    } else {
        if (self.nextMonthBlock) {
            self.nextMonthBlock();
        }
    }
}

#pragma mark - create View
- (void)setDate:(NSDate *)date{
    _date = date;
    [self createCalendarViewWith:date];
}

- (void)createCalendarViewWith:(NSDate *)date{
    
    CGFloat itemW     = self.frame.size.width / 7;
    CGFloat itemH     = self.frame.size.width / 7;
    
    // 1.year month
    if (self.headlabel == nil) {
        self.headlabel = [[UILabel alloc] init];
    }
    self.headlabel.text     = [NSString stringWithFormat:@"%li年%li月",(long)[self year:date],(long)[self month:date]];
    NSLog(@"%@", self.headlabel.text);
    self.headlabel.font     = [UIFont systemFontOfSize:14];
    self.headlabel.frame           = CGRectMake(0, 0, self.frame.size.width, itemH * 0.8);
    self.headlabel.textAlignment   = NSTextAlignmentCenter;
    self.headlabel.textColor = self.headColor;
    [self addSubview: self.headlabel];
    
    // 2.weekday
    NSArray *array = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    self.weekBg = [[UIView alloc] init];
    //    weekBg.backgroundColor = [UIColor orangeColor];
    self.weekBg.frame = CGRectMake(0, CGRectGetMaxY(self.headlabel.frame), self.frame.size.width, itemH * 0.6);
    [self addSubview:self.weekBg];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, PDScreeenW, 1.0)];
    line1.backgroundColor = LineCommanColorA;
    [self.weekBg addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.weekBg.frame.size.height - 1.0, PDScreeenW, 1.0)];
    line2.backgroundColor = LineCommanColorA;
    [self.weekBg addSubview:line2];
    
    for (int i = 0; i < 7; i++) {
        UILabel *week = [[UILabel alloc] init];
        week.text     = array[i];
        week.font     = [UIFont systemFontOfSize:14];
        week.frame    = CGRectMake(itemW * i, (itemH * 0.6 - 20) * 0.5, itemW, 20);
        week.textAlignment   = NSTextAlignmentCenter;
        week.backgroundColor = [UIColor clearColor];
        week.textColor       = self.weekDaysColor;
        [self.weekBg addSubview:week];
    }
    
    //  3.days (1-31)
    for (int i = 0; i < 42; i++) {
        
        int x = (i % 7) * itemW ;
        int y = (i / 7) * itemH + CGRectGetMaxY(self.weekBg.frame) + 5;
        
        UIButton *dayButton = _daysArray[i];
        dayButton.selected = NO;
        dayButton.frame = CGRectMake(x, y, itemW-6, itemH-6);
        
        dayButton.layer.cornerRadius = dayButton.frame.size.width / 2;
        dayButton.layer.masksToBounds = YES;
        dayButton.layer.borderWidth = 3.0 ;
        dayButton.layer.borderColor = [[UIColor clearColor] CGColor];
        
        dayButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        dayButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [dayButton addTarget:self action:@selector(logDate:) forControlEvents:UIControlEventTouchUpInside];
        
        NSInteger daysInLastMonth = [self totaldaysInMonth:[self lastMonth:date]];
        NSInteger daysInThisMonth = [self totaldaysInMonth:date];
        NSInteger firstWeekday    = [self firstWeekdayInThisMonth:date];
        
        NSInteger day = 0;
        
        
        if (i < firstWeekday) {
            day = daysInLastMonth - firstWeekday + i + 1;
            [self setStyle_BeyondThisMonth:dayButton];
            
        }else if (i > firstWeekday + daysInThisMonth - 1){
            day = i + 1 - firstWeekday - daysInThisMonth;
            [self setStyle_BeyondThisMonth:dayButton];
            
        }else{
            day = i - firstWeekday + 1;
            [dayButton setTitle:[NSString stringWithFormat:@"%li", (long)day] forState:UIControlStateNormal];
            [self setStyle_AfterToday:dayButton];
        }
        
        [dayButton setTitle:[NSString stringWithFormat:@"%li", (long)day] forState:UIControlStateNormal];
        dayButton.titleLabel.font = [UIFont systemFontOfSize:21];
        
        // this month
        if ([self month:date] == [self month:[NSDate date]]) {
            
            NSInteger todayIndex = [self day:date] + firstWeekday - 1;
            
            if (i < todayIndex && i >= firstWeekday) {
                //                [self setStyle_BeforeToday:dayButton];
            }else if(i ==  todayIndex){
                [self setStyle_Today:dayButton];
            }
            
        }
    }
}

#pragma mark - output date
-(void)logDate:(UIButton *)dayBtn
{
    
    dayBtn.selected = !dayBtn.selected;
    if (dayBtn.selected) {
        dayBtn.layer.borderColor = [self.dateColor CGColor];
    }else{

        dayBtn.layer.borderColor = [[UIColor clearColor] CGColor];
    }
    NSInteger day = [[dayBtn titleForState:UIControlStateNormal] integerValue];
    NSString *dayStr;
    if (day < 10) {
        dayStr = [NSString stringWithFormat:@"0%zd",day];
    }else{
        dayStr = [NSString stringWithFormat:@"%zd",day];
    }
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self.date];
    NSInteger month = [comp month];
    NSString *monthStr;
    if (month < 10) {
        monthStr = [NSString stringWithFormat:@"0%li",month];
    }else{
        monthStr = [NSString stringWithFormat:@"%li",month];
    }
    NSString *dateStr = [NSString stringWithFormat:@"%li-%@-%@", [comp year],monthStr, dayStr];
    
    if (self.calendarBlock) {
        self.calendarBlock(dateStr, dayBtn);
    }
    
}

#pragma mark - date button style

- (void)setStyle_BeyondThisMonth:(UIButton *)btn
{
    btn.enabled = NO;
    if (self.isShowOnlyMonthDays) {
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    } else {
        [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        
    }
}

- (void)setStyle_Today:(UIButton *)btn
{
    btn.selected = YES;
    self.defaultBtn = btn;
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:CommanColor forState:UIControlStateSelected];
    btn.layer.borderColor = [self.dateColor CGColor];
}

- (void)setStyle_AfterToday:(UIButton *)btn
{
    [btn setTitleColor:CommanColor forState:UIControlStateSelected];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    for (NSString *str in self.allDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10)  btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.allDaysColor;
    
        }
    }
    for (NSString *str in self.otherDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10) {
            btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        }
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.otherDaysColor;
        }
    }
    for (NSString *str in self.partDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10) {
            btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        }
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.partDaysColor;
        }
    }
}

#pragma mark - Lazy loading
- (NSMutableArray *)allDaysArr {
    if (!_allDaysArr) {
        _allDaysArr = [NSMutableArray array];
    }
    return _allDaysArr;
}
- (NSMutableArray *)otherDaysArr{
    if (!_otherDaysArr) {
        _otherDaysArr = [NSMutableArray array];
    }
    return _otherDaysArr;
}
- (NSMutableArray *)partDaysArr {
    if (!_partDaysArr) {
        _partDaysArr = [NSMutableArray array];
    }
    return _partDaysArr;
}

- (UIColor *)headColor {
    if (!_headColor) {
        _headColor = [UIColor blackColor];
    }
    return _headColor;
}

- (UIColor *)dateColor {
    if (!_dateColor) {
        _dateColor = [UIColor colorWithHexString:@"#7448B4" alpha:0.5];
    }
    return _dateColor;
}
- (UIColor *)weekDaysColor {
    if (!_weekDaysColor) {
        _weekDaysColor = [UIColor lightGrayColor];
    }
    return _weekDaysColor;
}

//一个月第一个周末
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
    NSDateComponents *component = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    [component setDay:1];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:component];
    NSUInteger firstWeekDay = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekDay - 1;
}

//总天数
- (NSInteger)totaldaysInMonth:(NSDate *)date{
    NSRange daysInOfMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInOfMonth.length;
}

#pragma mark - month +/-

- (NSDate *)lastMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

- (NSDate*)nextMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = +1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

#pragma mark - date get: day-month-year

- (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components day];
}

- (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components month];
}

- (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components year];
}
上一篇 下一篇

猜你喜欢

热点阅读