Browse files

Added setStartDate and setEndDate methods to the OCCalendarViewContro…

…ller, and created the supporting methods in all the subviews.
  • Loading branch information...
1 parent d85d5cf commit 636226c853015b07861cdd5616d0fe27bc3eaa30 @ocrickard committed Apr 15, 2012
View
23 Demo/OCCalendar/OCViewController.m
@@ -49,10 +49,33 @@ - (void)viewDidAppear:(BOOL)animated {
[self.view addSubview:[bgView autorelease]];
+ NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//Here's where the magic happens
calVC = [[OCCalendarViewController alloc] initAtPoint:CGPointMake(150, 50) inView:self.view arrowPosition:OCArrowPositionLeft];
calVC.delegate = self;
+ //Now we're going to optionally set the start and end date of a pre-selected range.
+ //This is totally optional.
+ NSDateComponents *dateParts = [[NSDateComponents alloc] init];
+ [dateParts setMonth:5];
+ [dateParts setYear:2012];
+ [dateParts setDay:8];
+
+ NSDate *sDate = [calendar dateFromComponents:dateParts];
+ [dateParts release];
+
+ dateParts = [[NSDateComponents alloc] init];
+ [dateParts setMonth:5];
+ [dateParts setYear:2012];
+ [dateParts setDay:14];
+
+ NSDate *eDate = [calendar dateFromComponents:dateParts];
+ [dateParts release];
+
+ [calVC setStartDate:sDate];
+ [calVC setEndDate:eDate];
+
+ //Add to the view.
[self.view addSubview:calVC.view];
}
View
3 OCCalendarView/OCCalendarView.h
@@ -47,4 +47,7 @@ typedef enum {
- (NSDate *)getStartDate;
- (NSDate *)getEndDate;
+- (void)setStartDate:(NSDate *)sDate;
+- (void)setEndDate:(NSDate *)eDate;
+
@end
View
113 OCCalendarView/OCCalendarView.m
@@ -27,10 +27,10 @@ - (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame arrowPosition:(OCArrowPosit
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
- NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
- NSDateComponents *dateParts = [calendar components:unitFlags fromDate:[NSDate date]];
- currentMonth = [dateParts month];
- currentYear = [dateParts year];
+ NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
+ NSDateComponents *dateParts = [calendar components:unitFlags fromDate:[NSDate date]];
+ currentMonth = [dateParts month];
+ currentYear = [dateParts year];
arrowPosition = arrowPos;
@@ -223,6 +223,111 @@ - (NSDate *)getEndDate {
return retDate;
}
+- (void)setStartDate:(NSDate *)sDate {
+ NSDateComponents *sComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:sDate];
+
+ if([sComponents month] != currentMonth) {
+ currentMonth = [sComponents month];
+ }
+ if([sComponents year] != currentYear) {
+ currentYear = [sComponents year];
+ }
+ int day = 1;
+ int month = currentMonth;
+ int year = currentYear;
+
+ //Get the first day of the month
+ NSDateComponents *dateParts = [[NSDateComponents alloc] init];
+ [dateParts setMonth:month];
+ [dateParts setYear:year];
+ [dateParts setDay:1];
+ NSDate *dateOnFirst = [calendar dateFromComponents:dateParts];
+ [dateParts release];
+ NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:dateOnFirst];
+ int weekdayOfFirst = [weekdayComponents weekday];
+
+ int numDaysInMonth = [calendar rangeOfUnit:NSDayCalendarUnit
+ inUnit:NSMonthCalendarUnit
+ forDate:dateOnFirst].length;
+
+ BOOL breakLoop = NO;
+ int countDays = 1;
+ for (int i = 0; i < 6; i++) {
+ if(breakLoop) {
+ break;
+ }
+ for(int j = 0; j < 7; j++) {
+ int dayNumber = i * 7 + j;
+ if(dayNumber >= (weekdayOfFirst - 1) && day <= numDaysInMonth) {
+ if(countDays == [sComponents day]) {
+ CGPoint thePoint = CGPointMake(j, i);
+ [selectionView setStartPoint:thePoint];
+ breakLoop = YES;
+ break;
+ }
+ ++countDays;
+ }
+ }
+ }
+
+ [daysView setMonth:currentMonth];
+ [daysView setYear:currentYear];
+ [daysView resetRows];
+ [daysView setNeedsDisplay];
+}
+
+- (void)setEndDate:(NSDate *)eDate {
+ NSDateComponents *eComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:eDate];
+
+ if([eComponents month] != currentMonth) {
+ currentMonth = [eComponents month];
+ }
+ if([eComponents year] != currentYear) {
+ currentYear = [eComponents year];
+ }
+ int day = 1;
+ int month = currentMonth;
+ int year = currentYear;
+
+ //Get the first day of the month
+ NSDateComponents *dateParts = [[NSDateComponents alloc] init];
+ [dateParts setMonth:month];
+ [dateParts setYear:year];
+ [dateParts setDay:1];
+ NSDate *dateOnFirst = [calendar dateFromComponents:dateParts];
+ [dateParts release];
+ NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:dateOnFirst];
+ int weekdayOfFirst = [weekdayComponents weekday];
+
+ int numDaysInMonth = [calendar rangeOfUnit:NSDayCalendarUnit
+ inUnit:NSMonthCalendarUnit
+ forDate:dateOnFirst].length;
+
+ BOOL breakLoop = NO;
+ int countDays = 1;
+ for (int i = 0; i < 6; i++) {
+ if(breakLoop) {
+ break;
+ }
+ for(int j = 0; j < 7; j++) {
+ int dayNumber = i * 7 + j;
+ if(dayNumber >= (weekdayOfFirst - 1) && day <= numDaysInMonth) {
+ if(countDays == [eComponents day]) {
+ CGPoint thePoint = CGPointMake(j, i);
+ [selectionView setEndPoint:thePoint];
+ breakLoop = YES;
+ break;
+ }
+ ++countDays;
+ }
+ }
+ }
+ [daysView setMonth:currentMonth];
+ [daysView setYear:currentYear];
+ [daysView resetRows];
+ [daysView setNeedsDisplay];
+}
+
- (void)drawRect:(CGRect)rect
{
//// Get day titles from current Locale
View
6 OCCalendarView/OCCalendarViewController.h
@@ -27,11 +27,17 @@
OCArrowPosition arrowPos;
UIView *parentView;
+
+ NSDate *startDate;
+ NSDate *endDate;
}
@property (nonatomic, assign) id <OCCalendarDelegate> delegate;
+@property (nonatomic, retain) NSDate *startDate;
+@property (nonatomic, retain) NSDate *endDate;
- (id)initAtPoint:(CGPoint)point inView:(UIView *)v;
- (id)initAtPoint:(CGPoint)point inView:(UIView *)v arrowPosition:(OCArrowPosition)ap;
+
@end
View
38 OCCalendarView/OCCalendarViewController.m
@@ -14,7 +14,7 @@ @interface OCCalendarViewController ()
@end
@implementation OCCalendarViewController
-@synthesize delegate;
+@synthesize delegate, startDate, endDate;
- (id)initAtPoint:(CGPoint)point inView:(UIView *)v arrowPosition:(OCArrowPosition)ap {
self = [super initWithNibName:nil bundle:nil];
@@ -57,16 +57,40 @@ - (void)loadView {
}
calView = [[OCCalendarView alloc] initAtPoint:insertPoint withFrame:CGRectMake(insertPoint.x - arrowPosX, insertPoint.y - 31.4, width, height) arrowPosition:arrowPos];
+ if(self.startDate) {
+ [calView setStartDate:startDate];
+ }
+ if(self.endDate) {
+ [calView setEndDate:endDate];
+ }
[self.view addSubview:[calView autorelease]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
+- (void)setStartDate:(NSDate *)sDate {
+ if(startDate) {
+ [startDate release];
+ startDate = nil;
+ }
+ startDate = [sDate retain];
+ [calView setStartDate:startDate];
+}
+
+- (void)setEndDate:(NSDate *)eDate {
+ if(endDate) {
+ [endDate release];
+ endDate = nil;
+ }
+ endDate = [eDate retain];
+ [calView setEndDate:endDate];
+}
+
- (void)removeCalView {
- NSDate *startDate = [calView getStartDate];
- NSDate *endDate = [calView getEndDate];
+ self.startDate = [calView getStartDate];
+ self.endDate = [calView getEndDate];
[calView removeFromSuperview];
calView = nil;
@@ -77,8 +101,6 @@ - (void)removeCalView {
[self.delegate completedWithStartDate:endDate endDate:startDate];
}
-
-
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if(calView) {
//Animate out the calendar view if it already exists
@@ -109,4 +131,10 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return YES;
}
+- (void)dealloc {
+ self.startDate = nil;
+ self.endDate = nil;
+ [super dealloc];
+}
+
@end
View
10 OCCalendarView/OCDaysView.m
@@ -82,12 +82,16 @@ - (void)drawRect:(CGRect)rect
inUnit:NSMonthCalendarUnit
forDate:prevDateOnFirst].length;
+ NSDateComponents *today = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
+
//Draw the text for each of those days.
for(int i = 0; i <= weekdayOfFirst-2; i++) {
int day = numDaysInPrevMonth - weekdayOfFirst + 2 + i;
NSString *str = [NSString stringWithFormat:@"%d", day];
+
+
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2);
CGRect dayHeader2Frame = CGRectMake((i)*hDiff, 0, 21, 14);
@@ -110,7 +114,11 @@ - (void)drawRect:(CGRect)rect
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2);
CGRect dayHeader2Frame = CGRectMake(j*hDiff, i*vDiff, 21, 14);
- [[UIColor whiteColor] setFill];
+ if([today day] == day && [today month] == month && [today year] == year) {
+ [[UIColor colorWithRed: 0.98 green: 0.24 blue: 0.09 alpha: 1] setFill];
+ } else {
+ [[UIColor whiteColor] setFill];
+ }
[str drawInRect: dayHeader2Frame withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
CGContextRestoreGState(context);
View
3 OCCalendarView/OCSelectionView.h
@@ -27,4 +27,7 @@
-(CGPoint)startPoint;
-(CGPoint)endPoint;
+-(void)setStartPoint:(CGPoint)sPoint;
+-(void)setEndPoint:(CGPoint)ePoint;
+
@end
View
14 OCCalendarView/OCSelectionView.m
@@ -159,4 +159,18 @@ -(CGPoint)endPoint {
return CGPointMake(endCellX, endCellY);
}
+-(void)setStartPoint:(CGPoint)sPoint {
+ startCellX = sPoint.x;
+ startCellY = sPoint.y;
+ selected = YES;
+ [self setNeedsDisplay];
+}
+
+-(void)setEndPoint:(CGPoint)ePoint {
+ endCellX = ePoint.x;
+ endCellY = ePoint.y;
+ selected = YES;
+ [self setNeedsDisplay];
+}
+
@end

0 comments on commit 636226c

Please sign in to comment.