Skip to content

Commit

Permalink
Added support for "Single Selection Mode"
Browse files Browse the repository at this point in the history
  • Loading branch information
dermdaly committed May 17, 2012
1 parent cbcb494 commit c2db11b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Demo/OCCalendar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
B1558BEF152648CB00326469 /* OCViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OCViewController.m; sourceTree = "<group>"; };
B1558BF2152648CB00326469 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/OCViewController.xib; sourceTree = "<group>"; };
B1D629951526926D005F7283 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
F311322B15651DEB0009A1B3 /* OCTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OCTypes.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -73,6 +74,7 @@
7B08B0DE152E203E00EACAC5 /* OCDaysView.m */,
7B08B0DF152E203E00EACAC5 /* OCSelectionView.h */,
7B08B0E0152E203E00EACAC5 /* OCSelectionView.m */,
F311322B15651DEB0009A1B3 /* OCTypes.h */,
);
name = OCCalendarView;
path = ../../OCCalendarView;
Expand Down
4 changes: 3 additions & 1 deletion OCCalendarView/OCCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
//

#import <UIKit/UIKit.h>

#import "OCTypes.h"
typedef enum {
OCArrowPositionLeft = -1,
OCArrowPositionCentered = 0,
OCArrowPositionRight = 1
} OCArrowPosition;


@class OCSelectionView;
@class OCDaysView;

Expand Down Expand Up @@ -50,4 +51,5 @@ typedef enum {
- (void)setStartDate:(NSDate *)sDate;
- (void)setEndDate:(NSDate *)eDate;

@property OCSelectionMode selectionMode;
@end
16 changes: 16 additions & 0 deletions OCCalendarView/OCCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
#import "OCDaysView.h"
#import <QuartzCore/QuartzCore.h>

@interface OCCalendarView () {
OCSelectionMode _selectionMode;
}
@end

@implementation OCCalendarView

-(OCSelectionMode) selectionMode {
return _selectionMode;
}

-(void) setSelectionMode:(OCSelectionMode)selectionMode {
_selectionMode = selectionMode;
// selectionView.selectionMode = _selectionMode;
}
- (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame {
return [self initAtPoint:p withFrame:frame arrowPosition:OCArrowPositionCentered];
}
Expand Down Expand Up @@ -353,6 +366,9 @@ - (void)setEndDate:(NSDate *)eDate {
[daysView setYear:currentYear];
[daysView resetRows];
[daysView setNeedsDisplay];
if(_selectionMode == OCSelectionSingleDate) {
[self setStartDate:[self getEndDate]];
}
}

- (void)drawRect:(CGRect)rect
Expand Down
3 changes: 2 additions & 1 deletion OCCalendarView/OCSelectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <UIKit/UIKit.h>

#import "OCTypes.h"
@interface OCSelectionView : UIView {
BOOL selected;
int startCellX;
Expand All @@ -29,5 +29,6 @@

-(void)setStartPoint:(CGPoint)sPoint;
-(void)setEndPoint:(CGPoint)ePoint;
@property(nonatomic, assign) OCSelectionMode selectionMode;

@end
24 changes: 21 additions & 3 deletions OCCalendarView/OCSelectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "OCSelectionView.h"

@implementation OCSelectionView
@synthesize selectionMode = _selectionMode;

- (id)initWithFrame:(CGRect)frame
{
Expand Down Expand Up @@ -80,9 +81,14 @@ - (void)drawRect:(CGRect)rect
} else if (!(startCellY > endCellY)) {
thisRowEndCell = 6;
}

CGFloat cornerRadius;
if(_selectionMode == OCSelectionSingleDate) {
cornerRadius = 0.0;
} else {
cornerRadius = 10.0;
}
//// selectedRect Drawing
UIBezierPath* selectedRectPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(MIN(thisRowStartCell, thisRowEndCell)*hDiff, i*vDiff, (ABS(thisRowEndCell-thisRowStartCell))*hDiff+20, 21) cornerRadius: 10];
UIBezierPath* selectedRectPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(MIN(thisRowStartCell, thisRowEndCell)*hDiff, i*vDiff, (ABS(thisRowEndCell-thisRowStartCell))*hDiff+20, 21) cornerRadius: cornerRadius];
CGContextSaveGState(context);
[selectedRectPath addClip];
CGContextDrawLinearGradient(context, gradient3, CGPointMake((MIN(thisRowStartCell, thisRowEndCell)+.5)*hDiff, (i+1)*vDiff), CGPointMake((MIN(thisRowStartCell, thisRowEndCell)+.5)*hDiff, i*vDiff), 0);
Expand All @@ -101,7 +107,7 @@ - (void)drawRect:(CGRect)rect
}
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
-(void) singleSelection:(NSSet *)touches {
selected = YES;

UITouch *touch = [touches anyObject];
Expand All @@ -115,9 +121,17 @@ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
endCellY = startCellY;

[self setNeedsDisplay];

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self singleSelection:touches];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(_selectionMode == OCSelectionSingleDate) {
[self singleSelection:touches];
return;
}
UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];
Expand All @@ -131,6 +145,10 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(_selectionMode == OCSelectionSingleDate) {
[self singleSelection:touches];
return;
}
UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];
Expand Down
12 changes: 12 additions & 0 deletions OCCalendarView/OCTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// OCTypes.h
// OCCalendar
//
// Created by Dermot Daly on 17/05/2012.
// Copyright (c) 2012 UC Berkeley. All rights reserved.
//

typedef enum {
OCSelectionSingleDate = 0,
OCSelectionDateRange
} OCSelectionMode;

0 comments on commit c2db11b

Please sign in to comment.