Skip to content

Instantly share code, notes, and snippets.

@WedgeSparda
Last active August 29, 2015 14:26
Show Gist options
  • Save WedgeSparda/1d08d38bbaa5bb5010a3 to your computer and use it in GitHub Desktop.
Save WedgeSparda/1d08d38bbaa5bb5010a3 to your computer and use it in GitHub Desktop.
Custom UIView with animation.
// Got from https://stackoverflow.com/questions/10809280/drawrect-circle-and-animate-size-color/10809496#10809496
#import <UIKit/UIKit.h>
// Use IB_DESIGNABLE if you want to see your custom view in Interface Builder (Storyboard or XIB)
IB_DESIGNABLE
@interface AnimationView : UIView
@end
// Got from https://stackoverflow.com/questions/10809280/drawrect-circle-and-animate-size-color/10809496#10809496
#import "AnimationView.h"
@implementation AnimationView
+ (Class)layerClass
{
return [CAShapeLayer class];
}
- (void)layoutSubviews
{
[self setLayerProperties];
// You can call this method anytime to trigger animations
// If you left it here, animations will start right away
[self attachAnimations];
}
- (void)setLayerProperties
{
CAShapeLayer *layer = (CAShapeLayer *)self.layer;
layer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
layer.fillColor = [UIColor colorWithHue:0 saturation:1 brightness:.8 alpha:1].CGColor;;
}
- (void)attachAnimations
{
[self attachPathAnimation];
[self attachColorAnimation];
}
- (void)attachPathAnimation
{
CABasicAnimation *animation = [self animationWithKeyPath:@"path"];
animation.toValue = (__bridge id)[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)].CGPath;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.layer addAnimation:animation forKey:animation.keyPath];
}
- (void)attachColorAnimation
{
CABasicAnimation *animation = [self animationWithKeyPath:@"fillColor"];
animation.fromValue = (__bridge id)[UIColor colorWithHue:0 saturation:.9 brightness:.9 alpha:1].CGColor;
[self.layer addAnimation:animation forKey:animation.keyPath];
}
- (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
animation.autoreverses = YES;
animation.repeatCount = HUGE_VALF;
animation.duration = 1;
return animation;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment