UICalendarView 时间多选控件

#import <UIKit/UIKit.h>

一、初始化

if (@available(iOS 16.0, *)) {


        UICalendarView *calen = [[UICalendarView alloc]initWithFrame:UIScreen.mainScreen.bounds];

        calen.backgroundColor = WHColor;

        calen.tintColor = [UIColor orangeColor];

        calen.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];

        calen.fontDesign = UIFontDescriptorSystemDesignRounded;

        calen.delegate = self;

        UICalendarSelectionMultiDate *multiDateSelection = [[UICalendarSelectionMultiDate alloc]initWithDelegate:self];

        calen.selectionBehavior = multiDateSelection;

        [self.view addSubview:calen];

    } else {

        // Fallback on earlier versions

    }

二、代理<UICalendarViewDelegate,UICalendarSelectionMultiDateDelegate>

-(void)calendarView:(UICalendarView *)calendarView didChangeVisibleDateComponentsFrom:(NSDateComponents *)previousDateComponents API_AVAILABLE(ios(16.0)){

    NSString *year = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.year];

    NSString *month = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.month];

    NSString *day = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.day];

    NSString *hour = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.hour];

    DLog(@"%@-%@-%@-%@",year,month,day,hour);

}

-(void)multiDateSelection:(UICalendarSelectionMultiDate *)selection didSelectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){

    NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];

    NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];

    NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];

    NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];

    DLog(@"%@-%@-%@-%@",year,month,day,hour);

}

-(void)multiDateSelection:(UICalendarSelectionMultiDate *)selection didDeselectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){

    NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];

    NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];

    NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];

    NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];

    DLog(@"YY%@-%@-%@-%@",year,month,day,hour);


}

-(BOOL)multiDateSelection:(UICalendarSelectionMultiDate *)selection canSelectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){

    NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];

    NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];

    NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];

    NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];

    DLog(@"TTS%@-%@-%@-%@",year,month,day,hour);

    return YES;

}

-(BOOL)multiDateSelection:(UICalendarSelectionMultiDate *)selection canDeselectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){

    NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];

    NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];

    NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];

    NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];

    DLog(@"TT%@-%@-%@-%@",year,month,day,hour);

    return YES;

}

三、控件图片

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容