Skip to content

Instantly share code, notes, and snippets.

@ariok
Created December 30, 2013 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariok/8185619 to your computer and use it in GitHub Desktop.
Save ariok/8185619 to your computer and use it in GitHub Desktop.
Animate a property of a layer and implicitly animate the result.
@dynamic theProperty; //The property should set as dynamic
- (id)initWithLayer:(id)layer {
if (self = [super initWithLayer:layer]) {
if ([layer isKindOfClass:[PieMask class]]) {
YourLayer *currentPresentation = (YourLayer *)layer;
self.theProperty = currentPresentation.theProperty;
//Add here any other properties needed for the animation
//I.E. self.anotherProperty = currentPresentation.anotherProperty;
}
}
return self;
}
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([key isEqualToString:@"theProperty"]) {
return YES;
}
return [super needsDisplayForKey:key];
}
-(id<CAAction>)actionForKey:(NSString *)event {
if ([event isEqualToString:@"theProperty"]{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:event];
anim.fromValue = [[self presentationLayer] valueForKey:event];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.duration = 1.0;
return anim;
}
return [super actionForKey:event];
}
- (void)drawInContext:(CGContextRef)ctx{
// Draw the layer using theProperty...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment