有没有用,而层暂停更新的CALayer位置的问题? [英] Is there an issue with updating a CALayer position while the layer is paused?

查看:169
本文介绍了有没有用,而层暂停更新的CALayer位置的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题与读取presentation位置,而它的暂停?

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

我试图暂停和恢复的CALayer 。一旦暂停的CALayer ,我想更新图层的位置与它目前的presentation位置。
当我尝试这样做,该层略有闪烁,一旦我恢复层。

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.

这是code我使用暂停和恢复的CALayer (基于<一个href=\"http://developer.apple.com/library/ios/#qa/qa1673/_index.html#//apple_ref/doc/uid/DTS40010053\">Technical Q&安培; A QA1673 通过苹果提供):

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

如果我取消注释 layer.position =((CALayer的*)[层presentationLayer])的位置;在 pauseLayer ,该层闪烁一次,我称之为 resumeLayer

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

这是我的动画code:

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

最好的问候

推荐答案

有不有,而它的暂停更新的CALayer 位置的问题。然而,自然它会给你提到的闪烁。那是因为你正在更新层的位置中旬动画。

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.

不要忘了,创建一个 CABasicAnimation 并将其添加到的CALayer 不改变图层的设置。它创建的动画的使用的层,但它不改变层

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.

因此​​,如果您是从动画效果层B,如果你想层动画结束后在B点出现,你需要这样的委托回调:

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

是的,这是 animationDidStart 。如果我们做到了使用 animationDidStop 那么你会看到另一个闪烁。该层将是B的动画的结束位置,那么你一看它的闪烁,然后你会看到它再次在B点。

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.

使用 animationDidStart 我们设定为 targetPosition ,即 B ,因为那是我们希望看到它完成。

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

现在,关于 QA1673 ,你用这个做的是动画速度设置为零,并获取当前的时间戳 CACurrentMediaTime()。在恢复,你把速度恢复正常,并应用在暂停时间产生的任何偏移。

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.

直到你得到它的窍门这一切似乎pretty混乱。我可以推荐一些阅读和视频?

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

绝对有<一读href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/CoreAnimationArchitecture.html\"相对=nofollow>核心动画渲染架构。

这是高度推荐影片是:

WWDC 2010会话424和425核心动画实践1部分和第2

WWDC 2011届421 Core Animation的要点

开发视频会议716 iPhone和Mac 核心动画技术

这篇关于有没有用,而层暂停更新的CALayer位置的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆