Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Last active August 29, 2015 14:00
Show Gist options
  • Save RuiAAPeres/11402456 to your computer and use it in GitHub Desktop.
Save RuiAAPeres/11402456 to your computer and use it in GitHub Desktop.
Sam Page's SparkRecording Implementation with Facebook's Pop
//The calculations are coming from [here](https://gist.github.com/d-ronnqvist/11266321) (thanks David!)
- (void)updateAnimations
{
CGFloat duration = self.duration * (1.f - [[self.progressLayers firstObject] strokeEnd]);
CGFloat strokeEndFinal = 1.f;
for (CAShapeLayer *progressLayer in self.progressLayers)
{
POPBasicAnimation *popEndAnimation = [POPBasicAnimation animation];
popEndAnimation.property = [POPAnimatableProperty propertyWithName:@"strokeEnd" initializer:^(POPMutableAnimatableProperty *prop) {
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj strokeEnd];
};
prop.writeBlock = ^(id obj, const CGFloat values[]) {
[obj setStrokeEnd:values[0]];
};
}];
popEndAnimation.duration = duration;
popEndAnimation.fromValue = @(progressLayer.strokeEnd);
popEndAnimation.toValue = @(strokeEndFinal);
[progressLayer pop_addAnimation:popEndAnimation forKey:@"strokeEndAnimation"];
CGFloat oldStrokeEnd = progressLayer.strokeEnd;
strokeEndFinal -= (oldStrokeEnd - progressLayer.strokeStart);
if (progressLayer != self.currentProgressLayer)
{
POPBasicAnimation *popStartAnimation = [POPBasicAnimation animation];
popStartAnimation.property = [POPAnimatableProperty propertyWithName:@"strokeStart" initializer:^(POPMutableAnimatableProperty *prop) {
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj strokeStart];
};
prop.writeBlock = ^(id obj, const CGFloat values[]) {
[obj setStrokeStart:values[0]];
};
}];
popStartAnimation.duration = duration;
popStartAnimation.fromValue = @(progressLayer.strokeStart);
popStartAnimation.toValue = @(strokeEndFinal);
[progressLayer pop_addAnimation:popStartAnimation forKey:@"strokeStartAnimation"];
}
}
POPBasicAnimation *backgroundLayerAnimation = [POPBasicAnimation animation];
backgroundLayerAnimation.property = [POPAnimatableProperty propertyWithName:@"strokeStart" initializer:^(POPMutableAnimatableProperty *prop) {
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj strokeStart];
};
prop.writeBlock = ^(id obj, const CGFloat values[]) {
[obj setStrokeStart:values[0]];
};
}];
backgroundLayerAnimation.duration = duration;
backgroundLayerAnimation.fromValue = @(self.backgroundLayer.strokeStart);
backgroundLayerAnimation.toValue = @(1.f);
[self.backgroundLayer pop_addAnimation:backgroundLayerAnimation forKey:@"strokeStartAnimation"];
}
- (void)removeAnimations
{
for (CAShapeLayer *progressLayer in self.progressLayers)
{
[progressLayer pop_removeAllAnimations];
}
[self.backgroundLayer pop_removeAllAnimations];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment