Skip to content

Instantly share code, notes, and snippets.

@rodchile
Created October 17, 2013 23:00
Show Gist options
  • Save rodchile/7033819 to your computer and use it in GitHub Desktop.
Save rodchile/7033819 to your computer and use it in GitHub Desktop.
#import <QuartzCore/QuartzCore.h>
static long long __color1 = 0x2ACE46;
static long long __color2 = 0x34AADC;
@interface MViewController ()
@property (nonatomic,strong) CALayer *layerColor;
@property (nonatomic,strong) UIColor *color1;
@property (nonatomic,strong) UIColor *color2;
@property (nonatomic,strong) UIButton *btn1;
@property (nonatomic,strong) UIButton *btn2;
@end
@implementation SKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect btn1Frame = CGRectMake(10.0, CGRectGetMidY(self.view.bounds), 100.0, 40.0);
CGRect btn2Frame = CGRectMake(150.0, CGRectGetMidY(self.view.bounds), 100.0, 40.0);
self.color1 = UIColorFromRGB(__color1);
self.color2 = UIColorFromRGB(__color2);
self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
self.btn1.frame = btn1Frame;
self.btn1.backgroundColor = self.color1;
[self.btn1 addTarget:self action:@selector(moveLayer:) forControlEvents:UIControlEventTouchUpInside];
self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
self.btn2.frame = btn2Frame;
self.btn2.backgroundColor = self.color2;
[self.btn2 addTarget:self action:@selector(moveLayer:) forControlEvents:UIControlEventTouchUpInside];
self.layerColor = [CALayer layer];
self.layerColor.frame = CGRectMake(10.0, CGRectGetMidY(btn1Frame) + 53.0,100.0,3.0);
self.layerColor.backgroundColor = self.color1.CGColor;
[self.view addSubview:self.btn1];
[self.view addSubview:self.btn2];
[self.view.layer addSublayer:self.layerColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)moveLayer:(UIButton *)sender
{
CGPoint toPoint = {.x = sender.frame.origin.x, .y = self.layerColor.frame.origin.y};
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:self.layerColor.frame.origin];
animation.toValue = [NSValue valueWithCGPoint:toPoint];
animation.duration = 0.3;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.layerColor addAnimation:animation forKey:@"position"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment