Skip to content

Commit

Permalink
Merge pull request Voley#1 from fouquet/master
Browse files Browse the repository at this point in the history
Added method to change index without calling completion block.
  • Loading branch information
Voley committed Oct 25, 2014
2 parents 6290a50 + e2dc258 commit 2e777a2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DVSwitcherExample/DVSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

- (void)setPressedHandler:(void (^)(NSUInteger index))handler;


// This method sets handler block that is getting called right before the switcher starts animating the transition

- (void)setWillBePressedHandler:(void (^)(NSUInteger index))handler;

- (void)selectIndex:(NSInteger)index animated:(BOOL)animated; // sets the index without calling the handler block

@end
55 changes: 36 additions & 19 deletions DVSwitcherExample/DVSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (instancetype)init
return self;
}

+ (instancetype)switchWithStringsArray:(NSArray *)strings
+ (instancetype)switchWithStringsArray:(NSArray *)strings
{
// to do
return [[DVSwitch alloc] initWithStringsArray:strings];
Expand All @@ -55,7 +55,7 @@ - (instancetype)initWithStringsArray:(NSArray *)strings
self.sliderColor = [UIColor whiteColor];
self.labelTextColorInsideSlider = [UIColor blackColor];
self.labelTextColorOutsideSlider = [UIColor whiteColor];

self.backgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = self.backgroundColor;
self.backgroundView.userInteractionEnabled = YES;
Expand Down Expand Up @@ -86,7 +86,7 @@ - (instancetype)initWithStringsArray:(NSArray *)strings
self.sliderView.backgroundColor = self.sliderColor;
self.sliderView.clipsToBounds = YES;
[self addSubview:self.sliderView];

self.onTopLabels = [[NSMutableArray alloc] init];

for (NSString *string in self.strings) {
Expand Down Expand Up @@ -128,19 +128,36 @@ - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated

if (animated) {

[self animateChangeToIndex:index];
[self animateChangeToIndex:index callHandler:YES];

} else {

[self changeToIndexWithoutAnimation:index callHandler:YES];
}
}

- (void)selectIndex:(NSInteger)index animated:(BOOL)animated
{
if (index > [self.strings count]) {
return;
}
self.selectedIndex = index;

if (animated) {

[self animateChangeToIndex:index callHandler:NO];

} else {

[self changeToIndexWithoutAnimation:index];
[self changeToIndexWithoutAnimation:index callHandler:NO];
}
}

- (void)layoutSubviews
{
self.backgroundView.layer.cornerRadius = self.cornerRadius;
self.sliderView.layer.cornerRadius = self.cornerRadius - 1;

self.backgroundView.backgroundColor = self.backgroundColor;
self.sliderView.backgroundColor = self.sliderColor;

Expand Down Expand Up @@ -170,7 +187,7 @@ - (void)layoutSubviews
}
}

- (void)animateChangeToIndex:(NSUInteger)selectedIndex
- (void)animateChangeToIndex:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler
{

if (self.willBePressedHandlerBlock) {
Expand All @@ -195,13 +212,13 @@ - (void)animateChangeToIndex:(NSUInteger)selectedIndex

} completion:^(BOOL finished) {

if (self.handlerBlock) {
if (self.handlerBlock && callHandler) {
self.handlerBlock(selectedIndex);
}
}];
}

- (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex
- (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler
{
if (self.willBePressedHandlerBlock) {
self.willBePressedHandlerBlock(selectedIndex);
Expand All @@ -221,15 +238,15 @@ - (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex
label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height);
}

if (self.handlerBlock) {
if (self.handlerBlock && callHandler) {
self.handlerBlock(selectedIndex);
}
}

- (void)handleRecognizerTap:(UITapGestureRecognizer *)rec
{
self.selectedIndex = rec.view.tag;
[self animateChangeToIndex:self.selectedIndex];
[self animateChangeToIndex:self.selectedIndex callHandler:YES];
}

- (void)sliderMoved:(UIPanGestureRecognizer *)rec
Expand All @@ -256,7 +273,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

self.sliderView.frame = CGRectMake(maxPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height);
}

CGRect newFrame = self.sliderView.frame;
CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0);

Expand All @@ -266,7 +283,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
}

} else if (rec.state == UIGestureRecognizerStateEnded || rec.state == UIGestureRecognizerStateCancelled || rec.state == UIGestureRecognizerStateFailed) {

NSMutableArray *distances = [[NSMutableArray alloc] init];

for (int i = 0; i < [self.strings count]; i++) {
Expand All @@ -285,7 +302,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

CGFloat sliderWidth = self.frame.size.width / [self.strings count];
CGFloat desiredX = sliderWidth * index + self.sliderOffset;

if (self.sliderView.frame.origin.x != desiredX) {

CGRect evenOlderFrame = self.sliderView.frame;
Expand All @@ -294,7 +311,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
CGFloat time = fabs(distance / 300);

[UIView animateWithDuration:time animations:^{

self.sliderView.frame = CGRectMake(desiredX, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height);

CGRect newFrame = self.sliderView.frame;
Expand All @@ -306,7 +323,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height);
}
} completion:^(BOOL finished) {

self.selectedIndex = index;

if (self.handlerBlock) {
Expand All @@ -317,9 +334,9 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

} else {

if (self.handlerBlock) {
self.handlerBlock(self.selectedIndex);
}
if (self.handlerBlock) {
self.handlerBlock(self.selectedIndex);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/DVSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

- (void)setPressedHandler:(void (^)(NSUInteger index))handler;


// This method sets handler block that is getting called right before the switcher starts animating the transition

- (void)setWillBePressedHandler:(void (^)(NSUInteger index))handler;

- (void)selectIndex:(NSInteger)index animated:(BOOL)animated; // sets the index without calling the handler block

@end
55 changes: 36 additions & 19 deletions Source/DVSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (instancetype)init
return self;
}

+ (instancetype)switchWithStringsArray:(NSArray *)strings
+ (instancetype)switchWithStringsArray:(NSArray *)strings
{
// to do
return [[DVSwitch alloc] initWithStringsArray:strings];
Expand All @@ -55,7 +55,7 @@ - (instancetype)initWithStringsArray:(NSArray *)strings
self.sliderColor = [UIColor whiteColor];
self.labelTextColorInsideSlider = [UIColor blackColor];
self.labelTextColorOutsideSlider = [UIColor whiteColor];

self.backgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = self.backgroundColor;
self.backgroundView.userInteractionEnabled = YES;
Expand Down Expand Up @@ -86,7 +86,7 @@ - (instancetype)initWithStringsArray:(NSArray *)strings
self.sliderView.backgroundColor = self.sliderColor;
self.sliderView.clipsToBounds = YES;
[self addSubview:self.sliderView];

self.onTopLabels = [[NSMutableArray alloc] init];

for (NSString *string in self.strings) {
Expand Down Expand Up @@ -128,19 +128,36 @@ - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated

if (animated) {

[self animateChangeToIndex:index];
[self animateChangeToIndex:index callHandler:YES];

} else {

[self changeToIndexWithoutAnimation:index callHandler:YES];
}
}

- (void)selectIndex:(NSInteger)index animated:(BOOL)animated
{
if (index > [self.strings count]) {
return;
}
self.selectedIndex = index;

if (animated) {

[self animateChangeToIndex:index callHandler:NO];

} else {

[self changeToIndexWithoutAnimation:index];
[self changeToIndexWithoutAnimation:index callHandler:NO];
}
}

- (void)layoutSubviews
{
self.backgroundView.layer.cornerRadius = self.cornerRadius;
self.sliderView.layer.cornerRadius = self.cornerRadius - 1;

self.backgroundView.backgroundColor = self.backgroundColor;
self.sliderView.backgroundColor = self.sliderColor;

Expand Down Expand Up @@ -170,7 +187,7 @@ - (void)layoutSubviews
}
}

- (void)animateChangeToIndex:(NSUInteger)selectedIndex
- (void)animateChangeToIndex:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler
{

if (self.willBePressedHandlerBlock) {
Expand All @@ -195,13 +212,13 @@ - (void)animateChangeToIndex:(NSUInteger)selectedIndex

} completion:^(BOOL finished) {

if (self.handlerBlock) {
if (self.handlerBlock && callHandler) {
self.handlerBlock(selectedIndex);
}
}];
}

- (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex
- (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler
{
if (self.willBePressedHandlerBlock) {
self.willBePressedHandlerBlock(selectedIndex);
Expand All @@ -221,15 +238,15 @@ - (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex
label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height);
}

if (self.handlerBlock) {
if (self.handlerBlock && callHandler) {
self.handlerBlock(selectedIndex);
}
}

- (void)handleRecognizerTap:(UITapGestureRecognizer *)rec
{
self.selectedIndex = rec.view.tag;
[self animateChangeToIndex:self.selectedIndex];
[self animateChangeToIndex:self.selectedIndex callHandler:YES];
}

- (void)sliderMoved:(UIPanGestureRecognizer *)rec
Expand All @@ -256,7 +273,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

self.sliderView.frame = CGRectMake(maxPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height);
}

CGRect newFrame = self.sliderView.frame;
CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0);

Expand All @@ -266,7 +283,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
}

} else if (rec.state == UIGestureRecognizerStateEnded || rec.state == UIGestureRecognizerStateCancelled || rec.state == UIGestureRecognizerStateFailed) {

NSMutableArray *distances = [[NSMutableArray alloc] init];

for (int i = 0; i < [self.strings count]; i++) {
Expand All @@ -285,7 +302,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

CGFloat sliderWidth = self.frame.size.width / [self.strings count];
CGFloat desiredX = sliderWidth * index + self.sliderOffset;

if (self.sliderView.frame.origin.x != desiredX) {

CGRect evenOlderFrame = self.sliderView.frame;
Expand All @@ -294,7 +311,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
CGFloat time = fabs(distance / 300);

[UIView animateWithDuration:time animations:^{

self.sliderView.frame = CGRectMake(desiredX, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height);

CGRect newFrame = self.sliderView.frame;
Expand All @@ -306,7 +323,7 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec
label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height);
}
} completion:^(BOOL finished) {

self.selectedIndex = index;

if (self.handlerBlock) {
Expand All @@ -317,9 +334,9 @@ - (void)sliderMoved:(UIPanGestureRecognizer *)rec

} else {

if (self.handlerBlock) {
self.handlerBlock(self.selectedIndex);
}
if (self.handlerBlock) {
self.handlerBlock(self.selectedIndex);
}
}
}
}
Expand Down

0 comments on commit 2e777a2

Please sign in to comment.