Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Created July 23, 2015 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjolnir/8f1aab5978c082818b23 to your computer and use it in GitHub Desktop.
Save fjolnir/8f1aab5978c082818b23 to your computer and use it in GitHub Desktop.
// Simply animates it's path along with bound change animations (if the path is changed along with the bounds that is)
@interface LEShapeLayer : CAShapeLayer
@end
@interface LEShapeView : UIView
@property(nonatomic, readonly) LEShapeLayer *shapeLayer;
@property(nonatomic, readonly) CGPathRef hitTestPath;
@end
#import "LEShapeLayer.h"
@implementation LEShapeLayer
- (void)addAnimation:(CAAnimation *)aAnim forKey:(NSString *)aKey
{
[super addAnimation:aAnim forKey:aKey];
if([aKey isEqualToString:@"bounds"] || [aKey isEqualToString:@"bounds.size"]) {
CABasicAnimation * const pathAnim = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnim.delegate = aAnim.delegate;
pathAnim.duration = aAnim.duration;
pathAnim.fillMode = aAnim.fillMode;
pathAnim.timingFunction = aAnim.timingFunction;
[self addAnimation:pathAnim forKey:@"path"];
CABasicAnimation * const contentAnim = [pathAnim copy];
contentAnim.keyPath = @"contents";
[self addAnimation:contentAnim forKey:@"contents"];
}
}
@end
@implementation LEShapeView
+ (Class)layerClass
{
return [LEShapeLayer class];
}
- (LEShapeLayer *)shapeLayer
{
return (LEShapeLayer *)self.layer;
}
- (CGPathRef)hitTestPath
{
return self.shapeLayer.path;
}
- (BOOL)pointInside:(CGPoint const)aPoint withEvent:(UIEvent * const)aEvent
{
return self.hitTestPath && CGPathContainsPoint(self.hitTestPath, NULL, aPoint, false);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment