Skip to content

Instantly share code, notes, and snippets.

@psobko
Created June 16, 2013 03:58
Show Gist options
  • Save psobko/5790705 to your computer and use it in GitHub Desktop.
Save psobko/5790705 to your computer and use it in GitHub Desktop.
Draws a box around a view and then animates it to grow vertically. Originally I was going to use this to display errors underneath UITexFields.
-(void)addBox
{
self.layer.masksToBounds = NO;
CALayer *errorLayer;
errorLayer = [CALayer layer];
errorLayer.backgroundColor = [UIColor blueColor].CGColor;
errorLayer.frame = CGRectMake(0,0, self.frame.size.width, self.frame.size.height);
errorLayer.cornerRadius = 0.0f;
CALayer *bgLayer = [CALayer layer];
bgLayer.frame = errorLayer.frame;
bgLayer.backgroundColor = [UIColor whiteColor].CGColor;
[self.layer insertSublayer:errorLayer atIndex:0];
[self.layer insertSublayer:bgLayer atIndex:1];
errorLayer.anchorPoint = CGPointMake(0.5,0.3);
CGRect startBounds = self.layer.bounds;
CGRect stopBounds = CGRectMake(0.0f, 0.0f, self.layer.frame.size.width+5, self.layer.frame.size.height*2.5);
CABasicAnimation* boundAnim = [CABasicAnimation animationWithKeyPath:@"bounds"];
boundAnim.fromValue = [NSValue valueWithCGRect:startBounds];
boundAnim.toValue = [NSValue valueWithCGRect:stopBounds];
CABasicAnimation* cornerAnim = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
cornerAnim.fromValue = [NSNumber numberWithFloat:0.0];
cornerAnim.toValue = [NSNumber numberWithFloat:5.0];
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:0.0];
fadeAnim.toValue = [NSNumber numberWithFloat:1.0];
//amo,atopm group
CAAnimationGroup* group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:fadeAnim, boundAnim,cornerAnim, nil];
group.duration = 0.15;
[errorLayer addAnimation:group forKey:@"BoxAnimation"];
errorLayer.bounds = stopBounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment