Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use self.view addsubview : TSQCalendarView? thanks #86

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TimesSquare.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
A806805216700FD70071C71E /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0510;
LastUpgradeCheck = 0630;
ORGANIZATIONNAME = Square;
};
buildConfigurationList = A806805516700FD70071C71E /* Build configuration list for PBXProject "TimesSquare" */;
Expand Down Expand Up @@ -234,6 +234,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
ONLY_ACTIVE_ARCH = YES;
PUBLIC_HEADERS_FOLDER_PATH = "include/$(PRODUCT_NAME)";
RUN_CLANG_STATIC_ANALYZER = YES;
SDKROOT = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0510"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,30 +23,42 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A81E05F71682A0E000E79A2B"
BuildableName = "TimesSquare Documentation"
BlueprintName = "TimesSquare Documentation"
ReferencedContainer = "container:TimesSquare.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
Expand Down
22 changes: 17 additions & 5 deletions TimesSquare.xcodeproj/xcshareddata/xcschemes/TimesSquare.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0510"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,10 +23,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -39,24 +39,36 @@
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A806805A16700FD70071C71E"
BuildableName = "libTimesSquare.a"
BlueprintName = "TimesSquare"
ReferencedContainer = "container:TimesSquare.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
Expand Down
2 changes: 1 addition & 1 deletion TimesSquare/TSQCalendarCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (void)layoutSubviews;
CGFloat extraSpace = (CGRectGetWidth(insetRect) - (self.daysInWeek - 1) * self.columnSpacing) - (increment * self.daysInWeek);

// Divide the extra space out over the outer columns in increments of the column spacing
NSInteger columnsWithExtraSpace = (NSInteger)fabsf(extraSpace / self.columnSpacing);
NSInteger columnsWithExtraSpace = (NSInteger)fabs(extraSpace / self.columnSpacing);
NSInteger columnsOnLeftWithExtraSpace = columnsWithExtraSpace / 2;
NSInteger columnsOnRightWithExtraSpace = columnsWithExtraSpace - columnsOnLeftWithExtraSpace;

Expand Down
1 change: 1 addition & 0 deletions TimesSquare/TSQCalendarRowCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@
*/
- (void)selectColumnForDate:(NSDate *)date;


@end
154 changes: 117 additions & 37 deletions TimesSquare/TSQCalendarRowCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ @interface TSQCalendarRowCell ()
@property (nonatomic, strong) NSArray *dayButtons;
@property (nonatomic, strong) NSArray *notThisMonthButtons;
@property (nonatomic, strong) UIButton *todayButton;
@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) NSArray *selectedButtons;

@property (nonatomic, assign) NSInteger indexOfTodayButton;
@property (nonatomic, assign) NSInteger indexOfSelectedButton;
@property (nonatomic, strong) NSMutableIndexSet *indexesOfSelectedButtons;

@property (nonatomic, strong) NSDateFormatter *dayFormatter;
@property (nonatomic, strong) NSDateFormatter *accessibilityFormatter;
Expand All @@ -39,9 +39,23 @@ - (id)initWithCalendar:(NSCalendar *)calendar reuseIdentifier:(NSString *)reuseI
return nil;
}

_indexesOfSelectedButtons = [NSMutableIndexSet indexSet];

return self;
}


- (void)prepareForReuse
{
[super prepareForReuse];

[self.selectedButtons enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL *stop) {
obj.hidden = YES;
}];

self.todayButton.hidden = YES;
}

- (void)configureButton:(UIButton *)button;
{
button.titleLabel.font = [UIFont boldSystemFontOfSize:19.f];
Expand Down Expand Up @@ -96,41 +110,49 @@ - (void)createTodayButton;
self.todayButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f / [UIScreen mainScreen].scale);
}

- (void)createSelectedButton;
- (void)createSelectedButtons;
{
self.selectedButton = [[UIButton alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:self.selectedButton];
[self configureButton:self.selectedButton];

[self.selectedButton setAccessibilityTraits:UIAccessibilityTraitSelected|self.selectedButton.accessibilityTraits];

self.selectedButton.enabled = NO;
[self.selectedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.selectedButton setBackgroundImage:[self selectedBackgroundImage] forState:UIControlStateNormal];
[self.selectedButton setTitleShadowColor:[UIColor colorWithWhite:0.0f alpha:0.75f] forState:UIControlStateNormal];
NSMutableArray *selectedButtons = [NSMutableArray arrayWithCapacity:self.daysInWeek];
for (NSUInteger index = 0; index < self.daysInWeek; index++) {

UIButton *button = [[UIButton alloc] initWithFrame:self.contentView.bounds];
button.hidden = YES;
[self.contentView addSubview:button];
[self configureButton:button];

[button setAccessibilityTraits:UIAccessibilityTraitSelected|button.accessibilityTraits];

button.enabled = NO;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[self selectedBackgroundImage] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithWhite:0.0f alpha:0.75f] forState:UIControlStateNormal];

button.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f / [UIScreen mainScreen].scale);

[selectedButtons addObject:button];
}

self.selectedButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f / [UIScreen mainScreen].scale);
self.indexOfSelectedButton = -1;
self.selectedButtons = selectedButtons;
self.indexesOfSelectedButtons = [NSMutableIndexSet indexSet];
}

- (void)setBeginningDate:(NSDate *)date;
{
_beginningDate = date;

if (!self.dayButtons) {
[self createDayButtons];
[self createNotThisMonthButtons];
[self createTodayButton];
[self createSelectedButton];
[self createSelectedButtons];
}

NSDateComponents *offset = [NSDateComponents new];
offset.day = 1;

self.todayButton.hidden = YES;
self.indexOfTodayButton = -1;
self.selectedButton.hidden = YES;
self.indexOfSelectedButton = -1;
[self.indexesOfSelectedButtons removeAllIndexes];

for (NSUInteger index = 0; index < self.daysInWeek; index++) {
NSString *title = [self.dayFormatter stringFromDate:date];
Expand All @@ -147,7 +169,7 @@ - (void)setBeginningDate:(NSDate *)date;
[self.notThisMonthButtons[index] setHidden:YES];

NSInteger thisDayMonth = thisDateComponents.month;
if (self.monthOfBeginningDate != thisDayMonth) {
if (self.monthOfBeginningDate != thisDayMonth || [self.calendarView.firstSelectableDate compare:date] == NSOrderedDescending) {
[self.notThisMonthButtons[index] setHidden:NO];
} else {

Expand Down Expand Up @@ -186,15 +208,43 @@ - (IBAction)dateButtonPressed:(id)sender;
NSDateComponents *offset = [NSDateComponents new];
offset.day = [self.dayButtons indexOfObject:sender];
NSDate *selectedDate = [self.calendar dateByAddingComponents:offset toDate:self.beginningDate options:0];
self.calendarView.selectedDate = selectedDate;

if (self.calendarView.selectionMode == TSQSelectionModeMultiple) {

NSMutableArray *tmp = [self.calendarView.selectedDates mutableCopy];

if ([tmp containsObject:selectedDate]) {
[tmp removeObject:selectedDate];
} else {
[tmp addObject:selectedDate];
}

self.calendarView.selectedDates = tmp;
} else {
self.calendarView.selectedDate = selectedDate;
}
}

- (IBAction)todayButtonPressed:(id)sender;
{
NSDateComponents *offset = [NSDateComponents new];
offset.day = self.indexOfTodayButton;
NSDate *selectedDate = [self.calendar dateByAddingComponents:offset toDate:self.beginningDate options:0];
self.calendarView.selectedDate = selectedDate;

if (self.calendarView.selectionMode == TSQSelectionModeMultiple) {

NSMutableArray *tmp = [self.calendarView.selectedDates mutableCopy];

if ([tmp containsObject:selectedDate]) {
[tmp removeObject:selectedDate];
} else {
[tmp addObject:selectedDate];
}

self.calendarView.selectedDates = tmp;
} else {
self.calendarView.selectedDate = selectedDate;
}
}

- (void)layoutSubviews;
Expand All @@ -219,38 +269,68 @@ - (void)layoutViewsForColumnAtIndex:(NSUInteger)index inRect:(CGRect)rect;
if (self.indexOfTodayButton == (NSInteger)index) {
self.todayButton.frame = rect;
}
if (self.indexOfSelectedButton == (NSInteger)index) {
self.selectedButton.frame = rect;
}

[self.indexesOfSelectedButtons enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if (idx == index) {
[self.selectedButtons[idx] setFrame:rect];
}
}];
}

- (void)selectColumnForDate:(NSDate *)date;
{
if (!date && self.indexOfSelectedButton == -1) {
if (!date && self.indexesOfSelectedButtons == nil) {
return;
}

NSInteger newIndexOfSelectedButton = -1;
NSInteger newIndexOfSelectedButton = NSNotFound;
if (date) {
NSInteger thisDayMonth = [self.calendar components:NSMonthCalendarUnit fromDate:date].month;
if (self.monthOfBeginningDate == thisDayMonth) {
newIndexOfSelectedButton = [self.calendar components:NSDayCalendarUnit fromDate:self.beginningDate toDate:date options:0].day;
if (newIndexOfSelectedButton >= (NSInteger)self.daysInWeek) {
newIndexOfSelectedButton = -1;
if (newIndexOfSelectedButton >= (NSInteger)self.daysInWeek || newIndexOfSelectedButton < 0) {
newIndexOfSelectedButton = NSNotFound;
}
}
}

// if this row has no selected date then unselect all dates
if (newIndexOfSelectedButton == NSNotFound) {
if (self.calendarView.selectionMode == TSQSelectionModeSingle) {
[self.selectedButtons enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL *stop) {
obj.hidden = YES;
}];
}
return;
}

self.indexOfSelectedButton = newIndexOfSelectedButton;
UIButton *button = self.selectedButtons[newIndexOfSelectedButton];

if (newIndexOfSelectedButton >= 0) {
self.selectedButton.hidden = NO;
NSString *newTitle = [self.dayButtons[newIndexOfSelectedButton] currentTitle];
[self.selectedButton setTitle:newTitle forState:UIControlStateNormal];
[self.selectedButton setTitle:newTitle forState:UIControlStateDisabled];
[self.selectedButton setAccessibilityLabel:[self.dayButtons[newIndexOfSelectedButton] accessibilityLabel]];
// remove previous selection if single selection mode
if (self.calendarView.selectionMode == TSQSelectionModeSingle && self.indexesOfSelectedButtons.count > 0) {
UIButton *previousSelectedButton = self.selectedButtons[self.indexesOfSelectedButtons.firstIndex];
previousSelectedButton.hidden = YES;

// remove all indexes
[self.indexesOfSelectedButtons removeAllIndexes];
}

if ([self.indexesOfSelectedButtons containsIndex:newIndexOfSelectedButton]) {
// remove index and un-select button
[self.indexesOfSelectedButtons removeIndex:newIndexOfSelectedButton];

button.hidden = YES;

} else {
self.selectedButton.hidden = YES;
// add index and select button
[self.indexesOfSelectedButtons addIndex:newIndexOfSelectedButton];

NSString *newTitle = [self.dayButtons[newIndexOfSelectedButton] currentTitle];
[button setTitle:newTitle forState:UIControlStateNormal];
[button setTitle:newTitle forState:UIControlStateDisabled];
[button setAccessibilityLabel:[self.dayButtons[newIndexOfSelectedButton] accessibilityLabel]];

button.hidden = NO;
}

[self setNeedsLayout];
Expand Down
Loading