Skip to content

Instantly share code, notes, and snippets.

@laiso
Created July 23, 2014 07:12
Show Gist options
  • Save laiso/9394d87de6b08da5cf3b to your computer and use it in GitHub Desktop.
Save laiso/9394d87de6b08da5cf3b to your computer and use it in GitHub Desktop.
神経系に訴えかける同意ボタン http://youtu.be/JfzJ_RkizIo
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self blink];
}
- (void)blink
{
NSTimeInterval duration = arc4random() % 5 / 10;
CABasicAnimation *color = [CABasicAnimation animationWithKeyPath:@"borderColor"];
UIColor *fromColor = [UIColor colorWithCGColor:self.button.layer.borderColor];
color.fromValue = (id)fromColor.CGColor;
UIColor *toColor = [self randomColor];
while ([toColor isEqual:fromColor]) {
toColor = [self randomColor];
}
color.toValue = (id)toColor.CGColor;
self.button.layer.borderColor = toColor.CGColor;
[self.button setTitleColor:toColor forState:UIControlStateNormal];
CABasicAnimation *width = [CABasicAnimation animationWithKeyPath:@"borderWidth"];
width.fromValue = @(self.button.layer.borderWidth);
width.toValue = @(arc4random() % 5 );
self.button.layer.borderWidth = [width.toValue floatValue];
CABasicAnimation *radius = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
width.fromValue = @(self.button.layer.cornerRadius);
width.toValue = @(arc4random() % 5 );
self.button.layer.cornerRadius = [width.toValue floatValue];
CAAnimationGroup *both = [CAAnimationGroup animation];
both.duration = duration;
both.animations = @[color, width, radius];
both.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.button.layer addAnimation:both forKey:@"color and width"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self blink];
});
}
- (UIColor *)randomColor
{
// https://gist.github.com/r3econ/9772706
// 0.0 to 1.0
CGFloat hue = ( arc4random() % 256 / 256.0 );
// 0.5 to 1.0, away from white
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
// 0.5 to 1.0, away from black.
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
return [UIColor colorWithHue:hue
saturation:saturation
brightness:brightness
alpha:1];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment