开发者

Is there an issue with updating a CALayer position while the layer is paused?

Is there an issue with reading the presentation position while it's paused?

I'm trying to pause and resume a CALayer. Once the CALayer is paused, I开发者_运维问答 want to update the layer's position with it's current presentation position. When I try do this, the layer flickers slightly once I resume the layer.

This is the code I'm using to pause and resume the CALayer (based on a Technical Q&A QA1673 supplied by Apple):

CFTimeInterval pausedTime;
void pauseLayer(CALayer *layer)
{
    pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
    layer.beginTime = 0;
//    layer.position = ((CALayer*)[layer presentationLayer]).position;
}
void resumeLayer(CALayer *layer)
{
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;

    CFTimeInterval _elapsedTimeSincePaused = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = _elapsedTimeSincePaused;
}

If I uncomment the layer.position = ((CALayer*)[layer presentationLayer]).position; in pauseLayer, the layer flickers once I call resumeLayer.

This is my animation code:

- (void) startAnimation:(id)sender
{
    layer10Animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    layer10Animation.duration = 1;
    layer10Animation.toValue = [NSNumber numberWithInt:300];
    layer10Animation.fromValue = [NSNumber numberWithInt:20];
    layer10Animation.repeatCount = 100;
    layer10Animation.autoreverses = YES;

    [layer10 addAnimation:layer10Animation forKey:nil];
}

Best regards


There's not an issue with updating aCALayerposition whilst it's paused. Naturally however it will give the flicker that you mention. That's because you are updating the layer's position mid animation.

Don't forget that creating aCABasicAnimationand adding it to aCALayerdoesn't change the layer's settings. It creates an animation using the layer, but it doesn't change the layer.

That's why after the animation has finished, you'll see the layer back in exactly the same position it was before.

Because of this, if you are animating a layer from A to B, if you want the layer to appear at B after the animation has finished, you'll need this delegate callback:

- (void)animationDidStart:(CAAnimation *)theAnimation
{ 
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];
    myLayer.position = targetPosition;    
    [CATransaction commit];
}

Yep, it'sanimationDidStart. If we did it usinganimationDidStopthen you would see another flicker. The layer would be in the animation's end position of B, then you'd see a flicker of it at A, and then you'd see it at B again.

UsinganimationDidStartwe set the position to be thetargetPosition, i.e.B because that's where we want to see it on completion.

Now, regarding QA1673, what you are doing with this is setting the animation speed to zero, and getting a timestamp of the currentCACurrentMediaTime(). On resume, you put the speed back to normal, and apply any offsets incurred during the pause time.

This all seems pretty confusing until you get the hang of it. Could I recommend some reading and videos?

Definitely have a read of Core Animation Rendering Architecture.

Videos that are highly recommended are:

WWDC 2010 Sessions 424 and 425 Core Animation in Practice Parts 1 and 2

WWDC 2011 Session 421 Core Animation Essentials

and

Developer Videos Session 716 Core Animation Techniques for iPhone and Mac

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜