Skip to content

Commit

Permalink
Merge pull request ocrickard#3 from markrickert/master
Browse files Browse the repository at this point in the history
Arrow definitions are now set with an enum.
  • Loading branch information
ocrickard committed Apr 5, 2012
2 parents b5fd177 + c859dac commit 9b0ea72
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Demo/OCCalendar/OCViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void)viewDidAppear:(BOOL)animated {


//Here's where the magic happens
calVC = [[OCCalendarViewController alloc] initAtPoint:CGPointMake(150, 50) inView:self.view];
calVC = [[OCCalendarViewController alloc] initAtPoint:CGPointMake(150, 50) inView:self.view arrowPosition:OCArrowPositionCentered];
calVC.delegate = self;
[self.view addSubview:calVC.view];
}
Expand Down
12 changes: 9 additions & 3 deletions OCCalendarView/OCCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#import <UIKit/UIKit.h>

typedef enum {
OCArrowPositionLeft = -1,
OCArrowPositionCentered = 0,
OCArrowPositionRight = 1
} OCArrowPosition;

@class OCSelectionView;
@class OCDaysView;

Expand All @@ -17,7 +23,6 @@
int currentMonth;
int currentYear;


BOOL selected;
int startCellX;
int startCellY;
Expand All @@ -34,9 +39,10 @@
}

- (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame;
- (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame arrowPosition:(OCArrowPosition)arrowPos;

//-1 means it's in the upper-left, 0 means it's in the middle, and 1 means it's in the upper-right.
- (void)setArrowPosition:(int)pos;
//Valid Positions: OCArrowPositionLeft, OCArrowPositionCentered, OCArrowPositionRight
- (void)setArrowPosition:(OCArrowPosition)pos;

- (NSDate *)getStartDate;
- (NSDate *)getEndDate;
Expand Down
96 changes: 51 additions & 45 deletions OCCalendarView/OCCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,59 @@
@implementation OCCalendarView

- (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame {
// CGRect frame = CGRectMake(p.x - 390*0.5, p.y - 31.4, 390, 270);
self = [super initWithFrame:frame];
if(self) {
self.backgroundColor = [UIColor clearColor];

calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
return [self initAtPoint:p withFrame:frame arrowPosition:OCArrowPositionCentered];
}

- (id)initAtPoint:(CGPoint)p withFrame:(CGRect)frame arrowPosition:(OCArrowPosition)arrowPos {
NSLog(@"Arrow Position: %u", arrowPos);

// CGRect frame = CGRectMake(p.x - 390*0.5, p.y - 31.4, 390, 270);
self = [super initWithFrame:frame];
if(self) {
self.backgroundColor = [UIColor clearColor];

calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateParts = [calendar components:unitFlags fromDate:[NSDate date]];
currentMonth = [dateParts month];
currentYear = [dateParts year];
arrowPosition = 0;
selected = NO;
startCellX = -1;
startCellY = -1;
endCellX = -1;
endCellY = -1;
hDiff = 43;
vDiff = 30;
selectionView = [[OCSelectionView alloc] initWithFrame:CGRectMake(66, 95, hDiff*7, vDiff*6)];
[self addSubview:selectionView];
daysView = [[OCDaysView alloc] initWithFrame:CGRectMake(65, 98, hDiff*7, vDiff*6)];
[daysView setYear:currentYear];
[daysView setMonth:currentMonth];
[daysView resetRows];
[self addSubview:daysView];
selectionView.frame = CGRectMake(66, 95, hDiff * 7, ([daysView addExtraRow] ? 6 : 5)*vDiff);
//Make the view really small and invisible
CGAffineTransform tranny = CGAffineTransformMakeScale(0.1, 0.1);
self.transform = tranny;
self.alpha = 0.0f;
//Animate in the view.
[UIView beginAnimations:@"animateInCalendar" context:nil];
[UIView setAnimationDuration:0.4f];
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
self.alpha = 1.0f;
[UIView commitAnimations];
}
return self;

arrowPosition = arrowPos;

selected = NO;
startCellX = -1;
startCellY = -1;
endCellX = -1;
endCellY = -1;

hDiff = 43;
vDiff = 30;

selectionView = [[OCSelectionView alloc] initWithFrame:CGRectMake(66, 95, hDiff*7, vDiff*6)];
[self addSubview:selectionView];

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

selectionView.frame = CGRectMake(66, 95, hDiff * 7, ([daysView addExtraRow] ? 6 : 5)*vDiff);

//Make the view really small and invisible
CGAffineTransform tranny = CGAffineTransformMakeScale(0.1, 0.1);
self.transform = tranny;
self.alpha = 0.0f;

//Animate in the view.
[UIView beginAnimations:@"animateInCalendar" context:nil];
[UIView setAnimationDuration:0.4f];
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
self.alpha = 1.0f;
[UIView commitAnimations];
}
return self;
}

- (void)setFrame:(CGRect)frame {
Expand Down Expand Up @@ -120,7 +126,7 @@ - (void)resetViews {
[UIView commitAnimations];
}

- (void)setArrowPosition:(int)pos {
- (void)setArrowPosition:(OCArrowPosition)pos {
arrowPosition = pos;
}

Expand Down Expand Up @@ -260,9 +266,9 @@ - (void)drawRect:(CGRect)rect

float arrowPosX = 208;

if(arrowPosition == -1) {
if(arrowPosition == OCArrowPositionLeft) {
arrowPosX = 67;
} else if(arrowPosition == 1) {
} else if(arrowPosition == OCArrowPositionRight) {
arrowPosX = 346;
}

Expand Down
19 changes: 11 additions & 8 deletions OCCalendarView/OCCalendarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import "OCCalendarView.h"

@class OCCalendarView;

Expand All @@ -17,18 +18,20 @@
@end

@interface OCCalendarViewController : UIViewController <UIGestureRecognizerDelegate> {
id <OCCalendarDelegate> delegate;

UILabel *toolTipLabel;
OCCalendarView *calView;

CGPoint insertPoint;

UIView *parentView;
id <OCCalendarDelegate> delegate;

UILabel *toolTipLabel;
OCCalendarView *calView;

CGPoint insertPoint;
OCArrowPosition arrowPos;

UIView *parentView;
}

@property (nonatomic, assign) id <OCCalendarDelegate> delegate;

- (id)initAtPoint:(CGPoint)point inView:(UIView *)v;
- (id)initAtPoint:(CGPoint)point inView:(UIView *)v arrowPosition:(OCArrowPosition)ap;

@end
21 changes: 12 additions & 9 deletions OCCalendarView/OCCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "OCCalendarViewController.h"
#import "OCCalendarView.h"
#import <QuartzCore/QuartzCore.h>

@interface OCCalendarViewController ()
Expand All @@ -17,13 +16,18 @@ @interface OCCalendarViewController ()
@implementation OCCalendarViewController
@synthesize delegate;

- (id)initAtPoint:(CGPoint)point inView:(UIView *)v arrowPosition:(OCArrowPosition)ap {
self = [super initWithNibName:nil bundle:nil];
if(self) {
insertPoint = point;
parentView = v;
arrowPos = ap;
}
return self;
}

- (id)initAtPoint:(CGPoint)point inView:(UIView *)v {
self = [super initWithNibName:nil bundle:nil];
if(self) {
insertPoint = point;
parentView = v;
}
return self;
return [self initAtPoint:point inView:v arrowPosition:OCArrowPositionCentered];
}

- (void)loadView {
Expand All @@ -34,7 +38,6 @@ - (void)loadView {
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];


//this view sits behind the calendar and receives touches. It tells the calendar view to disappear when tapped.
UIView *bgView = [[UIView alloc] initWithFrame:self.view.frame];
bgView.backgroundColor = [UIColor clearColor];
Expand All @@ -48,7 +51,7 @@ - (void)viewDidAppear:(BOOL)animated {
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:insertPoint withFrame:CGRectMake(insertPoint.x - width*0.5, insertPoint.y - 31.4, width, height) arrowPosition:arrowPos];
[self.view addSubview:[calView autorelease]];
}

Expand Down

0 comments on commit 9b0ea72

Please sign in to comment.