Created
October 17, 2013 20:53
-
-
Save bgerstle/7032037 to your computer and use it in GitHub Desktop.
Animating a custom property of a CALayer subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation PXScrubberView | |
+ (Class)layerClass | |
{ | |
return [PXScrubberLayer class]; | |
} | |
- (void)setFillHeight:(float)fillHeight animated:(BOOL)animated | |
{ | |
if (animated) { | |
CABasicAnimation* fillHeightAnimation = [CABasicAnimation animationWithKeyPath:@"fillHeight"]; | |
NSParameterAssert(fillHeightAnimation); | |
fillHeightAnimation.toValue = @(fillHeight); | |
fillHeightAnimation.fromValue = @(self.scrubberLayer.fillHeight); | |
fillHeightAnimation.duration = [[CATransaction valueForKey:kCATransactionAnimationDuration] doubleValue]; | |
fillHeightAnimation.fillMode = kCAFillModeForwards; | |
fillHeightAnimation.removedOnCompletion = YES; | |
fillHeightAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; | |
NSLog(@"Adding animation from %@ to %@", fillHeightAnimation.fromValue, fillHeightAnimation.toValue); | |
[self.layer addAnimation:fillHeightAnimation forKey:@"fillHeight"]; | |
} | |
self.scrubberLayer.fillHeight = fillHeight; | |
} | |
... | |
@end | |
@implementation PXScrubberLayer | |
- (void)setFillHeight:(float)fillHeight | |
{ | |
if (_fillHeight != fillHeight) { | |
_fillHeight = fillHeight; | |
NSLog(@"Set fill height: %f", _fillHeight); | |
[self setNeedsDisplay]; | |
} | |
} | |
- (void)setPositionValue:(float)positionValue | |
{ | |
if (MaybeSetClampedValue(&_positionValue, positionValue)) { | |
[self setNeedsDisplay]; | |
} | |
} | |
+ (BOOL)needsDisplayForKey:(NSString *)key | |
{ | |
static NSArray* customAnimatedKeys = nil; | |
if (!customAnimatedKeys) { | |
customAnimatedKeys = @[NSStringFromSelector(@selector(availableColor)), | |
NSStringFromSelector(@selector(emptyColor)), | |
NSStringFromSelector(@selector(positionColor)), | |
NSStringFromSelector(@selector(availableStart)), | |
NSStringFromSelector(@selector(availableEnd)), | |
NSStringFromSelector(@selector(positionValue)), | |
NSStringFromSelector(@selector(fillHeight))]; | |
} | |
if ([customAnimatedKeys containsObject:key]) { | |
return YES; | |
} else { | |
return [super needsDisplayForKey:key]; | |
} | |
} | |
... | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment