Skip to content

Commit

Permalink
Fixed month display bug, and corrected selection bug.
Browse files Browse the repository at this point in the history
The month view was off by a couple of months, and the starting day was
wrong.  I've also corrected the selection view to only update its
settings while the user's touch is inside of the bounds of the view.
  • Loading branch information
ocrickard committed Apr 5, 2012
1 parent a786186 commit 070687f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
Binary file not shown.
48 changes: 24 additions & 24 deletions OCCalendar/OCCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ - (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame {
[self addSubview:selectionView];

daysView = [[OCDaysView alloc] initWithFrame:CGRectMake(65, 98, hDiff*7, vDiff*6)];
[daysView setYear:2012];
[daysView setMonth:1];
[daysView setYear:currentYear];
[daysView setMonth:currentMonth];
[daysView resetRows];
[self addSubview:daysView];

Expand Down Expand Up @@ -125,7 +125,7 @@ - (void)setArrowPosition:(int)pos {
- (NSDate *)getStartDate {
CGPoint startPoint = [selectionView startPoint];

int day = 0;
int day = 1;
int month = currentMonth;
int year = currentYear;

Expand All @@ -139,28 +139,28 @@ - (NSDate *)getStartDate {
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:dateOnFirst];
int weekdayOfFirst = [weekdayComponents weekday];

//Map first day of month to a week starting on Monday
//as the weekday component defaults to 1->Sun, 2->Mon...
if(weekdayOfFirst == 1) {
weekdayOfFirst = 7;
} else {
--weekdayOfFirst;
}
// //Map first day of month to a week starting on Monday
// //as the weekday component defaults to 1->Sun, 2->Mon...
// if(weekdayOfFirst == 1) {
// weekdayOfFirst = 7;
// } else {
// --weekdayOfFirst;
// }

int numDaysInMonth = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:dateOnFirst].length;

if(startPoint.y == 0 && startPoint.x < weekdayOfFirst) {
day = startPoint.x - weekdayOfFirst+1;
if(startPoint.y == 0 && startPoint.x+1 < weekdayOfFirst) {
day = startPoint.x - weekdayOfFirst+2;
} else {
int countDays = 1;
for (int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
int dayNumber = i * 7 + j;
if(dayNumber >= (weekdayOfFirst - 3) && day <= numDaysInMonth) {
if(dayNumber >= (weekdayOfFirst - 1) && day <= numDaysInMonth) {
if(i == startPoint.y && j == startPoint.x) {
day = countDays-3;
day = countDays;
}
++countDays;
}
Expand All @@ -180,7 +180,7 @@ - (NSDate *)getStartDate {
- (NSDate *)getEndDate {
CGPoint endPoint = [selectionView endPoint];

int day = 0;
int day = 1;
int month = currentMonth;
int year = currentYear;

Expand All @@ -196,25 +196,25 @@ - (NSDate *)getEndDate {

//Map first day of month to a week starting on Monday
//as the weekday component defaults to 1->Sun, 2->Mon...
if(weekdayOfFirst == 1) {
weekdayOfFirst = 7;
} else {
--weekdayOfFirst;
}
// if(weekdayOfFirst == 1) {
// weekdayOfFirst = 7;
// } else {
// --weekdayOfFirst;
// }

int numDaysInMonth = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:dateOnFirst].length;
if(endPoint.y == 0 && endPoint.x < weekdayOfFirst) {
day = endPoint.x - weekdayOfFirst+1;
if(endPoint.y == 0 && endPoint.x+1 < weekdayOfFirst) {
day = endPoint.x - weekdayOfFirst+2;
} else {
int countDays = 1;
for (int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
int dayNumber = i * 7 + j;
if(dayNumber >= (weekdayOfFirst - 3) && day <= numDaysInMonth) {
if(dayNumber >= (weekdayOfFirst - 1) && day <= numDaysInMonth) {
if(i == endPoint.y && j == endPoint.x) {
day = countDays-3;
day = countDays;
}
++countDays;
}
Expand Down
4 changes: 2 additions & 2 deletions OCCalendar/OCCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive
//Recreate the calendar if it doesn't exist.

//CGPoint insertPoint = CGPointMake(200+130*0.5, 200+10);
CGPoint insertPoint = [touch locationInView:self.view];
CGPoint point = [touch locationInView:self.view];
int width = 390;
int height = 300;

calView = [[OCCalendarView alloc] initAtPoint:insertPoint withFrame:CGRectMake(insertPoint.x - width*0.5, insertPoint.y - 31.4, width, height)];
calView = [[OCCalendarView alloc] initAtPoint:point withFrame:CGRectMake(point.x - width*0.5, point.y - 31.4, width, height)];
[self.view addSubview:[calView autorelease]];
}

Expand Down
18 changes: 12 additions & 6 deletions OCCalendar/OCDaysView.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ - (void)drawRect:(CGRect)rect
[dateParts release];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:dateOnFirst];
int weekdayOfFirst = [weekdayComponents weekday];

NSLog(@"weekdayOfFirst:%d", weekdayOfFirst);

//Map first day of month to a week starting on Monday
//as the weekday component defaults to 1->Sun, 2->Mon...
if(weekdayOfFirst == 1) {
weekdayOfFirst = 7;
} else {
--weekdayOfFirst;
}
// if(weekdayOfFirst == 1) {
// weekdayOfFirst = 7;
// } else {
// --weekdayOfFirst;
// }

int numDaysInMonth = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:dateOnFirst].length;

NSLog(@"month:%d, numDaysInMonth:%d", currentMonth, numDaysInMonth);

CGContextRef context = UIGraphicsGetCurrentContext();

didAddExtraRow = NO;
Expand All @@ -75,7 +79,7 @@ - (void)drawRect:(CGRect)rect
for(int j = 0; j < 7; j++) {
int dayNumber = i * 7 + j;

if(dayNumber >= (weekdayOfFirst - 3) && day <= numDaysInMonth) {
if(dayNumber >= (weekdayOfFirst-1) && day <= numDaysInMonth) {
NSString *str = [NSString stringWithFormat:@"%d", day];

CGContextSaveGState(context);
Expand All @@ -97,10 +101,12 @@ - (void)drawRect:(CGRect)rect

- (void)setMonth:(int)month {
currentMonth = month;
[self setNeedsDisplay];
}

- (void)setYear:(int)year {
currentYear = year;
[self setNeedsDisplay];
}

- (void)resetRows {
Expand Down
20 changes: 12 additions & 8 deletions OCCalendar/OCSelectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,26 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];

endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;

[self setNeedsDisplay];
if(CGRectContainsPoint(self.bounds, point)) {
endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;

[self setNeedsDisplay];
}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];

endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;

[self setNeedsDisplay];
if(CGRectContainsPoint(self.bounds, point)) {
endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;

[self setNeedsDisplay];
}
}

-(void)resetSelection {
Expand Down

0 comments on commit 070687f

Please sign in to comment.