主要是使用了一个自定义的日历视图。
TdCalendarView.h
#import <UIKit/UIKit.h> @protocol CalendarViewDelegate; @interface TdCalendarView : UIView { CFGregorianDate currentMonthDate; CFGregorianDate currentSelectDate; CFAbsoluteTime currentTime; UIImageView* viewImageView; id<CalendarViewDelegate> calendarViewDelegate; int *monthFlagArray; } @property CFGregorianDate currentMonthDate; @property CFGregorianDate currentSelectDate; @property CFAbsoluteTime currentTime; @property (nonatomic, retain) UIImageView* viewImageView; @property (nonatomic, assign) id<CalendarViewDelegate> calendarViewDelegate; -(int)getDayCountOfaMonth:(CFGregorianDate)date; -(int)getMonthWeekday:(CFGregorianDate)date; -(int)getDayFlag:(int)day; -(void)setDayFlag:(int)day flag:(int)flag; -(void)clearAllDayFlag; @end @protocol CalendarViewDelegate<NSObject> @optional - (void) selectDateChanged:(CFGregorianDate) selectDate; - (void) monthChanged:(CFGregorianDate) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height; - (void) beforeMonthChange:(TdCalendarView*) calendarView willto:(CFGregorianDate) currentMonth; @end
TdCalendarView.m
#import "TdCalendarView.h" #import <QuartzCore/QuartzCore.h> const float headHeight=60; const float itemHeight=35; const float prevNextButtonSize=20; const float prevNextButtonSpaceWidth=15; const float prevNextButtonSpaceHeight=12; const float titleFontSize=30; const int weekFontSize=12; @implementation TdCalendarView @synthesize currentMonthDate; @synthesize currentSelectDate; @synthesize currentTime; @synthesize viewImageView; @synthesize calendarViewDelegate; -(void)initCalView{ currentTime=CFAbsoluteTimeGetCurrent(); currentMonthDate=CFAbsoluteTimeGetGregorianDate(currentTime,CFTimeZoneCopyDefault()); currentMonthDate.day=1; currentSelectDate.year=0; monthFlagArray=malloc(sizeof(int)*31); [self clearAllDayFlag]; } - (id)initWithCoder:(NSCoder *)coder { if (self = [super initWithCoder:coder]) { [self initCalView]; } return self; } - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initCalView]; } return self; } -(int)getDayCountOfaMonth:(CFGregorianDate)date{ switch (date.month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: if(date.year%4==0 && date.year%100!=0) return 29; else return 28; case 4: case 6: case 9: case 11: return 30; default: return 31; } } -(void)drawPrevButton:(CGPoint)leftTop { CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSetGrayStrokeColor(ctx,0,1); CGContextMoveToPoint (ctx, 0 + leftTop.x, prevNextButtonSize/2 + leftTop.y); CGContextAddLineToPoint (ctx, prevNextButtonSize + leftTop.x, 0 + leftTop.y); CGContextAddLineToPoint (ctx, prevNextButtonSize + leftTop.x, prevNextButtonSize + leftTop.y); CGContextAddLineToPoint (ctx, 0 + leftTop.x, prevNextButtonSize/2 + leftTop.y); CGContextFillPath(ctx); } -(void)drawNextButton:(CGPoint)leftTop { CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSetGrayStrokeColor(ctx,0,1); CGContextMoveToPoint (ctx, 0 + leftTop.x, 0 + leftTop.y); CGContextAddLineToPoint (ctx, prevNextButtonSize + leftTop.x, prevNextButtonSize/2 + leftTop.y); CGContextAddLineToPoint (ctx, 0 + leftTop.x, prevNextButtonSize + leftTop.y); CGContextAddLineToPoint (ctx, 0 + leftTop.x, 0 + leftTop.y); CGContextFillPath(ctx); } -(int)getDayFlag:(int)day { if(day>=1 && day<=31) { return *(monthFlagArray+day-1); } else return 0; } -(void)clearAllDayFlag { memset(monthFlagArray,0,sizeof(int)*31); } -(void)setDayFlag:(int)day flag:(int)flag { if(day>=1 && day<=31) { if(flag>0) *(monthFlagArray+day-1)=1; else if(flag<0) *(monthFlagArray+day-1)=-1; } } -(void)drawTopGradientBar{ CGContextRef ctx=UIGraphicsGetCurrentContext(); size_t num_locations = 3; CGFloat locations[3] = { 0.0, 0.5, 1.0}; CGFloat components[12] = { 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0 }; CGGradientRef myGradient; CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); CGPoint myStartPoint, myEndPoint; myStartPoint.x = headHeight; myStartPoint.y = 0.0; myEndPoint.x = headHeight; myEndPoint.y = headHeight; CGContextDrawLinearGradient(ctx,myGradient,myStartPoint, myEndPoint, 0); CGGradientRelease(myGradient); [self drawPrevButton:CGPointMake(prevNextButtonSpaceWidth,prevNextButtonSpaceHeight)]; [self drawNextButton:CGPointMake(self.frame.size.width-prevNextButtonSpaceWidth-prevNextButtonSize,prevNextButtonSpaceHeight)]; } -(void)drawTopBarWords{ int width=self.frame.size.width; int s_width=width/7; [[UIColor blackColor] set]; NSString *title_Month = [[NSString alloc] initWithFormat:@"%d年%d月",currentMonthDate.year,currentMonthDate.month]; int fontsize=[UIFont buttonFontSize]; UIFont *font = [UIFont systemFontOfSize:titleFontSize]; CGPoint location = CGPointMake(width/2 -2.5*titleFontSize, 0); [title_Month drawAtPoint:location withFont:font]; [title_Month release]; UIFont *weekfont=[UIFont boldSystemFontOfSize:weekFontSize]; fontsize+=3; fontsize+=20; [@"周一" drawAtPoint:CGPointMake(s_width*0+9,fontsize) withFont:weekfont]; [@"周二" drawAtPoint:CGPointMake(s_width*1+9,fontsize) withFont:weekfont]; [@"周三" drawAtPoint:CGPointMake(s_width*2+9,fontsize) withFont:weekfont]; [@"周四" drawAtPoint:CGPointMake(s_width*3+9,fontsize) withFont:weekfont]; [@"周五" drawAtPoint:CGPointMake(s_width*4+9,fontsize) withFont:weekfont]; [[UIColor redColor] set]; [@"周六" drawAtPoint:CGPointMake(s_width*5+9,fontsize) withFont:weekfont]; [@"周日" drawAtPoint:CGPointMake(s_width*6+9,fontsize) withFont:weekfont]; [[UIColor blackColor] set]; } -(void)drawGirdLines{ CGContextRef ctx=UIGraphicsGetCurrentContext(); int width=self.frame.size.width; int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1; int s_width=width/7; int tabHeight=row_Count*itemHeight+headHeight; CGContextSetGrayStrokeColor(ctx,0,1); CGContextMoveToPoint (ctx,0,headHeight); CGContextAddLineToPoint (ctx,0,tabHeight); CGContextStrokePath (ctx); CGContextMoveToPoint (ctx,width,headHeight); CGContextAddLineToPoint (ctx,width,tabHeight); CGContextStrokePath (ctx); for(int i=1;i<7;i++){ CGContextSetGrayStrokeColor(ctx,1,1); CGContextMoveToPoint(ctx, i*s_width-1, headHeight); CGContextAddLineToPoint( ctx, i*s_width-1,tabHeight); CGContextStrokePath(ctx); } for(int i=0;i<row_Count+1;i++){ CGContextSetGrayStrokeColor(ctx,1,1); CGContextMoveToPoint(ctx, 0, i*itemHeight+headHeight+3); CGContextAddLineToPoint( ctx, width,i*itemHeight+headHeight+3); CGContextStrokePath(ctx); CGContextSetGrayStrokeColor(ctx,0.3,1); CGContextMoveToPoint(ctx, 0, i*itemHeight+headHeight); CGContextAddLineToPoint( ctx, width,i*itemHeight+headHeight); CGContextStrokePath(ctx); } for(int i=1;i<7;i++){ CGContextSetGrayStrokeColor(ctx,0.3,1); CGContextMoveToPoint(ctx, i*s_width+2, headHeight); CGContextAddLineToPoint( ctx, i*s_width+2,tabHeight); CGContextStrokePath(ctx); } } -(int)getMonthWeekday:(CFGregorianDate)date { CFTimeZoneRef tz = CFTimeZoneCopyDefault(); CFGregorianDate month_date; month_date.year=date.year; month_date.month=date.month; month_date.day=1; month_date.hour=0; month_date.minute=0; month_date.second=1; return (int)CFAbsoluteTimeGetDayOfWeek(CFGregorianDateGetAbsoluteTime(month_date,tz),tz); } -(void)drawDateWords{ CGContextRef ctx=UIGraphicsGetCurrentContext(); int width=self.frame.size.width; int dayCount=[self getDayCountOfaMonth:currentMonthDate]; int day=0; int x=0; int y=0; int s_width=width/7; int curr_Weekday=[self getMonthWeekday:currentMonthDate]; UIFont *weekfont=[UIFont boldSystemFontOfSize:12]; for(int i=1;i<dayCount+1;i++) { day=i+curr_Weekday-2; x=day % 7; y=day / 7; NSString *date=[[[NSString alloc] initWithFormat:@"%2d",i] autorelease]; [date drawAtPoint:CGPointMake(x*s_width+15,y*itemHeight+headHeight) withFont:weekfont]; if([self getDayFlag:i]==1) { CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); [@"." drawAtPoint:CGPointMake(x*s_width+19,y*itemHeight+headHeight+6) withFont:[UIFont boldSystemFontOfSize:25]]; } else if([self getDayFlag:i]==-1) { CGContextSetRGBFillColor(ctx, 0, 8.5, 0.3, 1); [@"." drawAtPoint:CGPointMake(x*s_width+19,y*itemHeight+headHeight+6) withFont:[UIFont boldSystemFontOfSize:25]]; } CGContextSetRGBFillColor(ctx, 0, 0, 0, 1); } } - (void) movePrevNext:(int)isPrev{ currentSelectDate.year=0; [calendarViewDelegate beforeMonthChange:self willto:currentMonthDate]; int width=self.frame.size.width; int posX; if(isPrev==1) { posX=width; } else { posX=-width; } UIImage *viewImage; UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; viewImage= UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if(viewImageView==nil) { viewImageView=[[UIImageView alloc] initWithImage:viewImage]; viewImageView.center=self.center; [[self superview] addSubview:viewImageView]; } else { viewImageView.image=viewImage; } viewImageView.hidden=NO; viewImageView.transform=CGAffineTransformMakeTranslation(0, 0); self.hidden=YES; [self setNeedsDisplay]; self.transform=CGAffineTransformMakeTranslation(posX,0); float height; int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1; height=row_Count*itemHeight+headHeight; self.hidden=NO; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; self.transform=CGAffineTransformMakeTranslation(0,0); viewImageView.transform=CGAffineTransformMakeTranslation(-posX, 0); [UIView commitAnimations]; [calendarViewDelegate monthChanged:currentMonthDate viewLeftTop:self.frame.origin height:height]; } - (void)movePrevMonth{ if(currentMonthDate.month>1) currentMonthDate.month-=1; else { currentMonthDate.month=12; currentMonthDate.year-=1; } [self movePrevNext:0]; } - (void)moveNextMonth{ if(currentMonthDate.month<12) currentMonthDate.month+=1; else { currentMonthDate.month=1; currentMonthDate.year+=1; } [self movePrevNext:1]; } - (void) drawToday{ int x; int y; int day; CFGregorianDate today=CFAbsoluteTimeGetGregorianDate(currentTime, CFTimeZoneCopyDefault()); if(today.month==currentMonthDate.month && today.year==currentMonthDate.year) { int width=self.frame.size.width; int swidth=width/7; int weekday=[self getMonthWeekday:currentMonthDate]; day=today.day+weekday-2; x=day%7; y=day/7; CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1); CGContextMoveToPoint(ctx, x*swidth+1, y*itemHeight+headHeight); CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight); CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight+itemHeight); CGContextAddLineToPoint(ctx, x*swidth+1, y*itemHeight+headHeight+itemHeight); CGContextFillPath(ctx); CGContextSetRGBFillColor(ctx, 1, 1, 1, 1); UIFont *weekfont=[UIFont boldSystemFontOfSize:12]; NSString *date=[[[NSString alloc] initWithFormat:@"%2d",today.day] autorelease]; [date drawAtPoint:CGPointMake(x*swidth+15,y*itemHeight+headHeight) withFont:weekfont]; if([self getDayFlag:today.day]==1) { CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); [@"." drawAtPoint:CGPointMake(x*swidth+19,y*itemHeight+headHeight+6) withFont:[UIFont boldSystemFontOfSize:25]]; } else if([self getDayFlag:today.day]==-1) { CGContextSetRGBFillColor(ctx, 0, 8.5, 0.3, 1); [@"." drawAtPoint:CGPointMake(x*swidth+19,y*itemHeight+headHeight+6) withFont:[UIFont boldSystemFontOfSize:25]]; } } } - (void) drawCurrentSelectDate{ int x; int y; int day; int todayFlag; if(currentSelectDate.year!=0) { CFGregorianDate today=CFAbsoluteTimeGetGregorianDate(currentTime, CFTimeZoneCopyDefault()); if(today.year==currentSelectDate.year && today.month==currentSelectDate.month && today.day==currentSelectDate.day) todayFlag=1; else todayFlag=0; int width=self.frame.size.width; int swidth=width/7; int weekday=[self getMonthWeekday:currentMonthDate]; day=currentSelectDate.day+weekday-2; x=day%7; y=day/7; CGContextRef ctx=UIGraphicsGetCurrentContext(); if(todayFlag==1) CGContextSetRGBFillColor(ctx, 0, 0, 0.7, 1); else CGContextSetRGBFillColor(ctx, 0, 0, 1, 1); CGContextMoveToPoint(ctx, x*swidth+1, y*itemHeight+headHeight); CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight); CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight+itemHeight); CGContextAddLineToPoint(ctx, x*swidth+1, y*itemHeight+headHeight+itemHeight); CGContextFillPath(ctx); if(todayFlag==1) { CGContextSetRGBFillColor(ctx, 0, 0, 1, 1); CGContextMoveToPoint (ctx, x*swidth+4, y*itemHeight+headHeight+3); CGContextAddLineToPoint (ctx, x*swidth+swidth-1, y*itemHeight+headHeight+3); CGContextAddLineToPoint (ctx, x*swidth+swidth-1, y*itemHeight+headHeight+itemHeight-3); CGContextAddLineToPoint (ctx, x*swidth+4, y*itemHeight+headHeight+itemHeight-3); CGContextFillPath(ctx); } CGContextSetRGBFillColor(ctx, 1, 1, 1, 1); UIFont *weekfont=[UIFont boldSystemFontOfSize:12]; NSString *date=[[[NSString alloc] initWithFormat:@"%2d",currentSelectDate.day] autorelease]; [date drawAtPoint:CGPointMake(x*swidth+15,y*itemHeight+headHeight) withFont:weekfont]; if([self getDayFlag:currentSelectDate.day]!=0) { [@"." drawAtPoint:CGPointMake(x*swidth+19,y*itemHeight+headHeight+6) withFont:[UIFont boldSystemFontOfSize:25]]; } } } - (void) touchAtDate:(CGPoint) touchPoint{ int x; int y; int width=self.frame.size.width; int weekday=[self getMonthWeekday:currentMonthDate]; int monthDayCount=[self getDayCountOfaMonth:currentMonthDate]; x=touchPoint.x*7/width; y=(touchPoint.y-headHeight)/itemHeight; int monthday=x+y*7-weekday+2; if(monthday>0 && monthday<monthDayCount+1) { currentSelectDate.year=currentMonthDate.year; currentSelectDate.month=currentMonthDate.month; currentSelectDate.day=monthday; currentSelectDate.hour=0; currentSelectDate.minute=0; currentSelectDate.second=1; [calendarViewDelegate selectDateChanged:currentSelectDate]; [self setNeedsDisplay]; } } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { int width=self.frame.size.width; UITouch* touch=[touches anyObject]; CGPoint touchPoint=[touch locationInView:self]; //UIView* theview=[self hitTest:touchPoint withEvent:event]; if(touchPoint.x<40 && touchPoint.y<headHeight) [self movePrevMonth]; else if(touchPoint.x>width-40 && touchPoint.y<headHeight) [self moveNextMonth]; else if(touchPoint.y>headHeight) { [self touchAtDate:touchPoint]; } } - (void)drawRect:(CGRect)rect{ static int once=0; currentTime=CFAbsoluteTimeGetCurrent(); [self drawTopGradientBar]; [self drawTopBarWords]; [self drawGirdLines]; if(once==0) { once=1; float height; int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1; height=row_Count*itemHeight+headHeight; [calendarViewDelegate monthChanged:currentMonthDate viewLeftTop:self.frame.origin height:height]; [calendarViewDelegate beforeMonthChange:self willto:currentMonthDate]; } [self drawDateWords]; [self drawToday]; [self drawCurrentSelectDate]; } - (void)dealloc { [super dealloc]; free(monthFlagArray); } @end
相关推荐
在IT领域,自绘制日历控件是一种常见的用户界面元素,尤其在开发桌面应用或移动端应用时,开发者可能需要创建自定义的日历视图来满足特定的设计需求或功能要求。自绘制日历允许开发者拥有更大的自由度,可以设计出与...
在本文中,我们将深入探讨如何在Qt环境中创建一个自定义的日历控件,特别是通过“Qt之自绘制日历控件(三)”的实例来学习。这个控件不仅完全自绘,而且具备了月份切换和记录当前选中日期的功能,这使得它在实际应用中...
在Android平台上,绘制日历是一项常见的任务,尤其对于开发日程管理或时间规划类应用来说至关重要。本示例将深入探讨如何在Android中实现自定义的日历视图,包括基本的绘制逻辑、交互设计以及优化策略。 首先,我们...
本文以2019年全国各城市的空气质量观测数据为例,利用matplotlib、calmap、pyecharts绘制日历图和热力图。在绘图之前先利用pandas对空气质量数据进行处理。 2019年全国各城市空气质量观测数据来源于:...
java代码-使用java解决绘制日历的源代码 ——学习参考资料:仅用于个人学习使用!
前者是初次绘制时,如:初次想在窗体区域上绘制,此时窗体区域什么都没有,则目标图像是指窗体、图片中程序划定的要绘制区域所在像素。后者指在窗体或图像上已经绘制过图像。 源图像 源图像是指最新、最近绘制的图像...
此外,为了提高代码的可读性和可维护性,可以将不同部分的功能封装到单独的函数中,如计算某个月的第一天是星期几(可以使用蔡勒公式),绘制日历的头部(月份和年份),绘制每一天的日期,以及绘制边框等。...
origin软件是一款专业的制图软件,功能强大,该文件的日历图的插件插件
你可以通过继承`View`或`ViewGroup`类来实现这些视图,并重写`onDraw()`方法来绘制日历布局。使用`Canvas`对象进行画布绘制,包括日期的矩形框、文字、线条等元素。 2. **日期计算与管理**:在绘制日历之前,需要...
这通常涉及绘制日历的各个部分(如日、周、月),并处理用户交互事件,如点击日期、滚动月份等。 3. **信号与槽机制**: 在Qt中,信号和槽是实现对象间通信的关键机制。在自定义日历控件时,我们可以定义信号来表示...
在`OnPaint`方法中,我们通常会获取`CPaintDC`对象,这是设备上下文的一个包装,然后使用GDI(Graphics Device Interface)函数来绘制日历的各个部分,如日期、星期等。例如,我们可以使用`TextOut`来绘制文本,`...
例如,我们可以创建一个自定义的`CalendarView`类,继承自`View`或`ViewGroup`,并重写其`onDraw()`方法来绘制日历的各个部分。在这个过程中,我们需要处理日历的布局、日期的计算以及折叠动画的实现。折叠效果可以...
Easyx是一个为C语言设计的图形库,它使得在C语言中进行图形绘制和用户交互变得更加简单。这个程序包包含了完整的源代码和必要的资源文件,方便开发者学习和修改。 【描述】提到的"在vc6.0环境下编程"意味着我们需要...
在`onDraw()`方法中,开发者需要编写代码来绘制日历的各个部分,如日期方格、星期列、月份标题等。可以使用`Canvas`进行绘制,包括设置颜色、字体、线条等。同时,需要处理好绘制的顺序,确保正确的重绘效果。 5. ...
在`CalendarCanvas`中,开发者通过`canvas`元素和相关API绘制日历,可以实现自定义的样式和交互效果。 ### 2. 多点触控(Multi-Touch) 多点触控是现代触摸设备支持的一种输入方式,允许用户同时使用多个手指进行...
1. 自定义View:创建一个继承自View或ViewGroup的类,重写onDraw()方法来绘制日历的布局。你需要考虑以下几点: - 日历布局:确定日历的展示方式,例如网格布局、流式布局等。 - 字体样式:设置日期字体的大小、...
一旦有了农历数据,就可以在绘制日历时加入农历日期,或者创建一个新的控件专门显示农历信息。 在VS2013中创建这样的工程,你需要先创建一个MFC对话框应用程序项目,然后添加自定义控件。在资源视图中,可以添加新...
- 需要覆盖`onDraw()`方法来绘制日历的各个元素,如日期、星期等,使用`Canvas`对象进行绘制。 - 可能还需要重写`onMeasure()`方法来确定View的尺寸,确保其适应不同屏幕大小。 2. **滑动隐藏效果**: - 要实现...