Skip to content

Commit

Permalink
Pass weak reference to animation blocks, highlight subviews on select…
Browse files Browse the repository at this point in the history
…ion/highlight, add -dateForIndex: method.
  • Loading branch information
robbdimitrov committed Jan 31, 2014
1 parent 49ef47d commit 8818581
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
4 changes: 2 additions & 2 deletions RDVCalendarView.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "RDVCalendarView"
s.version = "1.0.2"
s.version = "1.0.3"
s.summary = "Highly customizable calendarView and calendarViewController for iOS"
s.description = "RDVCalendarView is iPad and iPhone compatible. Everything can be customized.
Supports landscape and portrait orientations and is fully localized using NSLocale."
s.homepage = "https://github.com/robbdimitrov/RDVCalendarView"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Robert Dimitrov" => "[email protected]" }
s.platform = :ios, '5.0'
s.source = { :git => "https://github.com/robbdimitrov/RDVCalendarView.git", :tag => "v1.0.2" }
s.source = { :git => "https://github.com/robbdimitrov/RDVCalendarView.git", :tag => "v1.0.3" }
s.source_files = 'RDVCalendarView', 'RDVCalendarView/**/*.{h,m}'
s.framework = 'UIKit', 'CoreGraphics', 'Foundation'
s.requires_arc = true
Expand Down
33 changes: 22 additions & 11 deletions RDVCalendarView/RDVCalendarDayCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,27 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
_highlighted = NO;

if ([self selectionStyle] != RDVCalendarDayCellSelectionStyleNone) {
__weak RDVCalendarDayCell *weakSelf = self;

void (^block)() = ^{
if (selected) {
[[self backgroundView] setAlpha:0.0f];
[[self selectedBackgroundView] setAlpha:1.0f];
[[weakSelf backgroundView] setAlpha:0.0f];
[[weakSelf selectedBackgroundView] setAlpha:1.0f];
} else {
[[self backgroundView] setAlpha:1.0f];
[[self selectedBackgroundView] setAlpha:0.0f];
[[weakSelf backgroundView] setAlpha:1.0f];
[[weakSelf selectedBackgroundView] setAlpha:0.0f];
}
for (id subview in [[weakSelf contentView] subviews]) {
if ([subview respondsToSelector:@selector(setHighlighted:)]) {
[subview setHighlighted:selected];
}
}
[[self textLabel] setHighlighted:selected];
};

if (animated) {
[UIView animateWithDuration:0.25f animations:block];
} else {
block();

}
}
}
Expand All @@ -130,15 +135,21 @@ - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
_selected = NO;

if ([self selectionStyle] != RDVCalendarDayCellSelectionStyleNone) {
__weak RDVCalendarDayCell *weakSelf = self;

void (^block)() = ^{
if (highlighted) {
[[self backgroundView] setAlpha:0.0f];
[[self selectedBackgroundView] setAlpha:1.0f];
[[weakSelf backgroundView] setAlpha:0.0f];
[[weakSelf selectedBackgroundView] setAlpha:1.0f];
} else {
[[self backgroundView] setAlpha:1.0f];
[[self selectedBackgroundView] setAlpha:0.0f];
[[weakSelf backgroundView] setAlpha:1.0f];
[[weakSelf selectedBackgroundView] setAlpha:0.0f];
}
for (id subview in [[weakSelf contentView] subviews]) {
if ([subview respondsToSelector:@selector(setHighlighted:)]) {
[subview setHighlighted:highlighted];
}
}
[[self textLabel] setHighlighted:highlighted];
};

if (animated) {
Expand Down
5 changes: 5 additions & 0 deletions RDVCalendarView/RDVCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ typedef NS_ENUM(NSInteger, RDVCalendarViewDayCellSeparatorType) {
*/
- (RDVCalendarDayCell *)dayCellForIndex:(NSInteger)index;

/**
* Returns the NSDate at the specified index.
*/
- (NSDate *)dateForIndex:(NSInteger)index;

#pragma mark - Managing Selections

/**
Expand Down
80 changes: 52 additions & 28 deletions RDVCalendarView/RDVCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (id)initWithFrame:(CGRect)frame

// Setup calendar

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSCalendar *calendar = [self calendar];

_currentDay = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]];

Expand Down Expand Up @@ -286,8 +286,18 @@ - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier {

#pragma mark - Set up a calendar view

- (NSCalendar *)calendar {
static NSCalendar *calendar = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
});
return calendar;
}

- (void)setupWeekDays {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSCalendar *calendar = [self calendar];
NSInteger firstWeekDay = [calendar firstWeekday] - 1;

// We need an NSDateFormatter to have access to the localized weekday strings
Expand Down Expand Up @@ -390,7 +400,7 @@ - (void)setSelectedDate:(NSDate *)selectedDate {
NSDate *oldDate = [self selectedDate];

if (![oldDate isEqualToDate:selectedDate]) {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSCalendar *calendar = [self calendar];

if (![self selectedDay]) {
[self setSelectedDay:[[NSDateComponents alloc] init]];
Expand Down Expand Up @@ -420,7 +430,7 @@ - (void)setSelectedDate:(NSDate *)selectedDate {

- (NSDate *)selectedDate {
if ([self selectedDay]) {
return [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[self selectedDay]];
return [[self calendar] dateFromComponents:[self selectedDay]];
}
return nil;
}
Expand All @@ -445,21 +455,23 @@ - (RDVCalendarDayCell *)dayCellForIndex:(NSInteger)index {
}
}

[dayCell prepareForReuse];
[dayCell.textLabel setText:[NSString stringWithFormat:@"%d", index + 1]];

if (index + 1 == [self currentDay].day &&
[self month].month == [self currentDay].month &&
[self month].year == [self currentDay].year) {
if (![[self visibleCells] containsObject:dayCell]) {
[dayCell prepareForReuse];
[dayCell.textLabel setText:[NSString stringWithFormat:@"%d", index + 1]];

if (index + 1 == [self currentDay].day &&
[self month].month == [self currentDay].month &&
[self month].year == [self currentDay].year) {
[[dayCell backgroundView] setBackgroundColor:[self currentDayColor]];
} else {
[[dayCell backgroundView] setBackgroundColor:[self normalDayColor]];
} else {
[[dayCell backgroundView] setBackgroundColor:[self normalDayColor]];
}

[[dayCell selectedBackgroundView] setBackgroundColor:[self selectedDayColor]];

[dayCell setNeedsLayout];
}

[[dayCell selectedBackgroundView] setBackgroundColor:[self selectedDayColor]];

[dayCell setNeedsLayout];

return dayCell;
}

Expand All @@ -477,6 +489,17 @@ - (NSInteger)indexForDayCellAtPoint:(CGPoint)point {
return 0;
}

- (NSDate *)dateForIndex:(NSInteger)index {
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:[[self month] year]];
[dateComponents setMonth:[[self month] month]];
[dateComponents setDay:(index + 1)];

NSDate *date = [[self calendar] dateFromComponents:dateComponents];

return date;
}

#pragma mark - Managing selections

- (NSInteger)indexForSelectedDayCell {
Expand All @@ -496,10 +519,10 @@ - (void)selectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated {
NSDateComponents *selectedDayComponents = [[NSDateComponents alloc] init];
[selectedDayComponents setMonth:[[self month] month]];
[selectedDayComponents setYear:[[self month] year]];
[selectedDayComponents setDay:index + 1];
[selectedDayComponents setDay:(index + 1)];

if ([[self delegate] respondsToSelector:@selector(calendarView:willSelectDate:)]) {
[[self delegate] calendarView:self willSelectDate:[[[self month] calendar]
[[self delegate] calendarView:self willSelectDate:[[self calendar]
dateFromComponents:selectedDayComponents]];
}

Expand All @@ -513,7 +536,7 @@ - (void)selectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated {
[self setSelectedDay:selectedDayComponents];

if ([[self delegate] respondsToSelector:@selector(calendarView:didSelectDate:)]) {
[[self delegate] calendarView:self didSelectDate:[[[self month] calendar]
[[self delegate] calendarView:self didSelectDate:[[self calendar]
dateFromComponents:[self selectedDay]]];
}

Expand All @@ -540,21 +563,21 @@ - (void)deselectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated {
#pragma mark - Helper methods

- (NSInteger)numberOfWeeks {
return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
}

- (NSInteger)numberOfDays {
return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[self firstDay]].length;
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[self firstDay]].length;
}

- (NSInteger)numberOfDaysInFirstWeek {
return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
}

- (RDVCalendarDayCell *)viewAtLocation:(CGPoint)location {
Expand Down Expand Up @@ -605,6 +628,7 @@ - (void)showNextMonth {
- (void)currentLocaleDidChange:(NSNotification *)notification {
[self setupWeekDays];
[self updateMonthLabelMonth:[self month]];
[self setNeedsLayout];
}

#pragma mark - Touch handling
Expand Down

0 comments on commit 8818581

Please sign in to comment.