Skip to content

Instantly share code, notes, and snippets.

@vibrazy
Created July 31, 2014 19:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vibrazy/a59af0c6e55ca4b68b34 to your computer and use it in GitHub Desktop.
Save vibrazy/a59af0c6e55ca4b68b34 to your computer and use it in GitHub Desktop.
Facebook Pop Animating CGPath - CoreAnimation + Pop
CGFloat height = 100.f;
UIBezierPath *straightPath = [UIBezierPath bezierPath];
[straightPath moveToPoint:CGPointMake(0, 0)];
[straightPath addCurveToPoint:CGPointMake(0, height) controlPoint1:CGPointMake(0, height * 0.5) controlPoint2:CGPointMake(0, height * 0.5)];
UIBezierPath *bendiPath = [UIBezierPath bezierPath];
[bendiPath moveToPoint:CGPointMake(0, 0)];
[bendiPath addCurveToPoint:CGPointMake(0, height) controlPoint1:CGPointMake(40, height * 0.5) controlPoint2:CGPointMake(40, height * 0.5)];
UIBezierPath *bendiPath2 = [UIBezierPath bezierPath];
[bendiPath2 moveToPoint:CGPointMake(0, 0)];
[bendiPath2 addCurveToPoint:CGPointMake(0, height) controlPoint1:CGPointMake(-40, height * 0.5) controlPoint2:CGPointMake(-40, height * 0.5)];
shape = [CAShapeLayer layer];
shape.path = straightPath.CGPath;
shape.strokeColor = [UIColor blackColor].CGColor;
shape.lineWidth = 10.f;
shape.fillColor = nil;
shape.position = self.view.center;
[self.view.layer addSublayer:shape];
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"path"];
keyFrame.values = @[(__bridge id)(straightPath.CGPath),(__bridge id)(bendiPath.CGPath),(__bridge id)(straightPath.CGPath),(__bridge id)(bendiPath2.CGPath),(__bridge id)(straightPath.CGPath)];
keyFrame.duration = 100.0f;
keyFrame.additive = YES;
keyFrame.removedOnCompletion = NO;
keyFrame.fillMode = kCAFillModeForwards;
[shape addAnimation:keyFrame forKey:nil];
shape.speed = 0.0;
// return;
self.pop = [POPAnimatableProperty propertyWithName:@"timeOffset" initializer:^(POPMutableAnimatableProperty *prop) {
// read value
prop.readBlock = ^(CAShapeLayer *obj, CGFloat values[]) {
values[0] = obj.timeOffset;
};
// write value
prop.writeBlock = ^(CAShapeLayer *obj, const CGFloat values[]) {
obj.timeOffset = values[0];
};
// dynamics threshold
prop.threshold = 0.1;
}];
self.anim = [POPSpringAnimation animation];
_anim.fromValue = @(0.0);
_anim.toValue = @(100.f);
_anim.springBounciness = 20.1;
_anim.springSpeed = 20.4;
_anim.dynamicsTension = 200;
_anim.dynamicsFriction = 100.f;
_anim.dynamicsMass = 1.f;
_anim.property = self.pop;
[shape pop_addAnimation:_anim forKey:nil];
@bitmess
Copy link

bitmess commented May 9, 2017

better add

shape.timeOffset = 0;

when initialize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment