Skip to content

Commit

Permalink
RTL support (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighthawk committed Jan 12, 2022
1 parent d2d79e1 commit 5831f14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 10 additions & 2 deletions Classes/ASSingleWeekView.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ - (void)rebuildView
UIView *subview = self.subviews[index];
[subview removeFromSuperview];
}
CGFloat widthPerItem = CGRectGetWidth(self.frame) / 7;
CGFloat totalWidth = CGRectGetWidth(self.frame);
CGFloat widthPerItem = totalWidth / 7;
CGFloat itemHeight = CGRectGetHeight(self.frame);
for (NSUInteger dayIndex = 0; dayIndex < 7; dayIndex++) {
NSDate *date = [self dateByAddingDays:dayIndex toDate:self.startDate];
CGRect frame = CGRectMake(dayIndex * widthPerItem, 0, widthPerItem, itemHeight);
CGRect frame;
if (self.effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight) {
frame = CGRectMake(dayIndex * widthPerItem, 0, widthPerItem, itemHeight);
} else {
frame = CGRectMake(totalWidth - dayIndex * widthPerItem - widthPerItem, 0, widthPerItem, itemHeight);
}


UIView *view = [self.delegate singleWeekView:self
viewForDate:date
withFrame:frame];
Expand Down
12 changes: 7 additions & 5 deletions Classes/ASWeekSelectorView.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
CGRect leftFrame = week0.frame;
CGRect middleFrame = week1.frame;
CGRect rightFrame = week2.frame;
NSInteger multiplier = self.effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ? 1 : -1;

if (offset.x <= 0) {
// 0 and 1 move right
Expand All @@ -204,14 +205,14 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
self.singleWeekViews[2] = week1;

// 2 get's updated to -1
week2.startDate = [self dateByAddingDays:-7 toDate:week0.startDate];
week2.startDate = [self dateByAddingDays:-7*multiplier toDate:week0.startDate];
week2.frame = leftFrame;
self.singleWeekViews[0] = week2;

if ([self.delegate respondsToSelector:@selector(weekSelectorDidSwipe:)]) {
[self.delegate weekSelectorDidSwipe:self];
}
NSDate *date = [self dateByAddingDays:-7 toDate:self.selectedDate];
NSDate *date = [self dateByAddingDays:-7*multiplier toDate:self.selectedDate];
[self userWillSelectDate:date];
[self userDidSelectDate:date];

Expand All @@ -223,14 +224,14 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
self.singleWeekViews[1] = week2;

// 0 get's updated to 3
week0.startDate = [self dateByAddingDays:7 toDate:week2.startDate];
week0.startDate = [self dateByAddingDays:7*multiplier toDate:week2.startDate];
week0.frame = rightFrame;
self.singleWeekViews[2] = week0;

if ([self.delegate respondsToSelector:@selector(weekSelectorDidSwipe:)]) {
[self.delegate weekSelectorDidSwipe:self];
}
NSDate *date = [self dateByAddingDays:7 toDate:self.selectedDate];
NSDate *date = [self dateByAddingDays:7*multiplier toDate:self.selectedDate];
[self userWillSelectDate:date];
[self userDidSelectDate:date];
}
Expand Down Expand Up @@ -434,7 +435,8 @@ - (void)rebuildWeeks

// now we can build the #WEEKS subvies
for (NSUInteger index = 0; index < WEEKS; index++) {
CGRect frame = CGRectMake(index * width, 0, width, height);
NSUInteger effectiveIndex = self.effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ? index : WEEKS - index - 1;
CGRect frame = CGRectMake(effectiveIndex * width, 0, width, height);
ASSingleWeekView *singleView = [[ASSingleWeekView alloc] initWithFrame:frame];
singleView.delegate = self;
singleView.startDate = date; // needs to be set AFTER delegate
Expand Down

0 comments on commit 5831f14

Please sign in to comment.