flashlib
4/29/2014 - 2:43 PM

Sam Page's SparkRecording Implementation with Facebook's Pop

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];
}