Skip to content

Commit

Permalink
Fixed bug where double tapping during animation would cause a graphic…
Browse files Browse the repository at this point in the history
…al glitch.
  • Loading branch information
Mike Ahmarani authored and Mike Ahmarani committed Feb 18, 2012
1 parent cea43e2 commit dbdc47d
Showing 1 changed file with 96 additions and 91 deletions.
187 changes: 96 additions & 91 deletions MAConfirmButton/MAConfirmButton.m
Expand Up @@ -110,114 +110,118 @@ - (id)initWithTitle:(NSString *)titleString confirm:(NSString *)confirmString{
return self;
}

- (void)toggle{

self.titleLabel.alpha = 0;

CGSize size;

if(disabled){
[self setTitle:disabled forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.6 alpha:1] forState:UIControlStateNormal];
[self setTitleShadowColor:[UIColor colorWithWhite:1 alpha:1] forState:UIControlStateNormal];
self.titleLabel.shadowOffset = CGSizeMake(0, 1);
size = [disabled sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}else if(selected){
[self setTitle:confirm forState:UIControlStateNormal];
size = [confirm sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}else{
[self setTitle:title forState:UIControlStateNormal];
size = [title sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}

size.width += kPadding;
float offset = size.width - self.frame.size.width;
- (void)toggle{
if(self.userInteractionEnabled){
self.userInteractionEnabled = NO;
self.titleLabel.alpha = 0;

CGSize size;

if(disabled){
[self setTitle:disabled forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.6 alpha:1] forState:UIControlStateNormal];
[self setTitleShadowColor:[UIColor colorWithWhite:1 alpha:1] forState:UIControlStateNormal];
self.titleLabel.shadowOffset = CGSizeMake(0, 1);
size = [disabled sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}else if(selected){
[self setTitle:confirm forState:UIControlStateNormal];
size = [confirm sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}else{
[self setTitle:title forState:UIControlStateNormal];
size = [title sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
}

[CATransaction begin];
[CATransaction setAnimationDuration:0.25];
[CATransaction setCompletionBlock:^{
//Readjust button frame for new touch area, move layers back now that animation is done
size.width += kPadding;
float offset = size.width - self.frame.size.width;

CGRect frameRect = self.frame;
switch(self.toggleAnimation){
case MAConfirmButtonToggleAnimationLeft:
frameRect.origin.x = frameRect.origin.x - offset;
break;
case MAConfirmButtonToggleAnimationRight:
break;
case MAConfirmButtonToggleAnimationCenter:
frameRect.origin.x = frameRect.origin.x - offset/2.0;
break;
default:
break;
}
frameRect.size.width = frameRect.size.width + offset;
self.frame = frameRect;
[CATransaction begin];
[CATransaction setAnimationDuration:0.25];
[CATransaction setCompletionBlock:^{
//Readjust button frame for new touch area, move layers back now that animation is done

[CATransaction setDisableActions:YES];
for(CALayer *layer in self.layer.sublayers){
CGRect rect = layer.frame;
CGRect frameRect = self.frame;
switch(self.toggleAnimation){
case MAConfirmButtonToggleAnimationLeft:
rect.origin.x = rect.origin.x+offset;
frameRect.origin.x = frameRect.origin.x - offset;
break;
case MAConfirmButtonToggleAnimationRight:
break;
case MAConfirmButtonToggleAnimationCenter:
rect.origin.x = rect.origin.x+offset/2.0;
frameRect.origin.x = frameRect.origin.x - offset/2.0;
break;
default:
break;
}
frameRect.size.width = frameRect.size.width + offset;
self.frame = frameRect;

[CATransaction setDisableActions:YES];
[CATransaction setCompletionBlock:^{
self.userInteractionEnabled = YES;
}];
for(CALayer *layer in self.layer.sublayers){
CGRect rect = layer.frame;
switch(self.toggleAnimation){
case MAConfirmButtonToggleAnimationLeft:
rect.origin.x = rect.origin.x+offset;
break;
case MAConfirmButtonToggleAnimationRight:
break;
case MAConfirmButtonToggleAnimationCenter:
rect.origin.x = rect.origin.x+offset/2.0;
break;
default:
break;
}

layer.frame = rect;
}
[CATransaction commit];

layer.frame = rect;
}
[CATransaction commit];
self.titleLabel.alpha = 1;
[self setNeedsLayout];
}];

self.titleLabel.alpha = 1;
[self setNeedsLayout];
}];
UIColor *greenColor = [UIColor colorWithRed:0.439 green:0.741 blue:0.314 alpha:1.];

UIColor *greenColor = [UIColor colorWithRed:0.439 green:0.741 blue:0.314 alpha:1.];
//Animate color change
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
colorAnimation.removedOnCompletion = NO;
colorAnimation.fillMode = kCAFillModeForwards;

//Animate color change
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
colorAnimation.removedOnCompletion = NO;
colorAnimation.fillMode = kCAFillModeForwards;
if(disabled){
colorAnimation.fromValue = (id)greenColor.CGColor;
colorAnimation.toValue = (id)[UIColor colorWithWhite:0.85 alpha:1].CGColor;
}else{
colorAnimation.fromValue = selected ? (id)tint.CGColor : (id)greenColor.CGColor;
colorAnimation.toValue = selected ? (id)greenColor.CGColor : (id)tint.CGColor;
}

if(disabled){
colorAnimation.fromValue = (id)greenColor.CGColor;
colorAnimation.toValue = (id)[UIColor colorWithWhite:0.85 alpha:1].CGColor;
}else{
colorAnimation.fromValue = selected ? (id)tint.CGColor : (id)greenColor.CGColor;
colorAnimation.toValue = selected ? (id)greenColor.CGColor : (id)tint.CGColor;
}
[colorLayer addAnimation:colorAnimation forKey:@"colorAnimation"];

[colorLayer addAnimation:colorAnimation forKey:@"colorAnimation"];

//Animate layer scaling
for(CALayer *layer in self.layer.sublayers){
CGRect rect = layer.frame;

switch(self.toggleAnimation){
case MAConfirmButtonToggleAnimationLeft:
rect.origin.x = rect.origin.x-offset;
break;
case MAConfirmButtonToggleAnimationRight:
break;
case MAConfirmButtonToggleAnimationCenter:
rect.origin.x = rect.origin.x-offset/2.0;
break;
default:
break;
}
rect.size.width = rect.size.width+offset;
layer.frame = rect;
}
//Animate layer scaling
for(CALayer *layer in self.layer.sublayers){
CGRect rect = layer.frame;

[CATransaction commit];
switch(self.toggleAnimation){
case MAConfirmButtonToggleAnimationLeft:
rect.origin.x = rect.origin.x-offset;
break;
case MAConfirmButtonToggleAnimationRight:
break;
case MAConfirmButtonToggleAnimationCenter:
rect.origin.x = rect.origin.x-offset/2.0;
break;
default:
break;
}
rect.size.width = rect.size.width+offset;
layer.frame = rect;
}

[self setNeedsDisplay];
[CATransaction commit];
[self setNeedsDisplay];
}
}

- (void)setupLayers{
Expand Down Expand Up @@ -256,7 +260,7 @@ - (void)setSelected:(BOOL)s{
}

- (void)disableWithTitle:(NSString *)disabledString{
self.disabled = [disabledString retain];
self.disabled = [disabledString retain];
[self toggle];
}

Expand Down Expand Up @@ -290,15 +294,15 @@ - (void)lighten{
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(!disabled && !confirmed){
if(!disabled && !confirmed && self.userInteractionEnabled){
[self darken];
}
[super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if(!disabled && !confirmed){
if(!disabled && !confirmed && self.userInteractionEnabled){
if(!CGRectContainsPoint(self.frame, [[touches anyObject] locationInView:self.superview])){ //TouchUpOutside (Cancelled Touch)
[self lighten];
[super touchesCancelled:touches withEvent:event];
Expand All @@ -322,11 +326,12 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}

- (void)cancel{
if(cancelOverlay){
if(cancelOverlay && self.userInteractionEnabled){
[cancelOverlay removeFromSuperview];
cancelOverlay = nil;
}
self.selected = NO;
}


@end

0 comments on commit dbdc47d

Please sign in to comment.