1

我正在使用 Apple 的以下代码来暂停和恢复动画。

单击暂停按钮时,我首先暂停动画,然后显示一个弹出窗口。单击弹出窗口中的播放按钮时,我首先删除弹出窗口然后恢复动画。我面临的问题是有时它无法恢复。动画保持暂停。当我快速暂停并无延迟地反复恢复动画时,就会发生这种情况。

- (void)pauseAnimation:(CALayer *)layer
{
    if (m_pCustomImageBtnObj != nil)
    {
        CFTimeInterval pausedTime =
            [layer convertTime:CACurrentMediaTime() fromLayer:nil];

        layer.speed = 0.0;
        layer.timeOffset = pausedTime;
    }
}

- (void)resumeAnimation
{
    if (m_pCustomImageBtnObj != nil)
    {
        CFTimeInterval pausedTime = [m_pCustomImageBtnObj.layer timeOffset];
        m_pCustomImageBtnObj.layer.speed = 1.0;
        m_pCustomImageBtnObj.layer.timeOffset = 0.0;
        m_pCustomImageBtnObj.layer.beginTime = 0.0;

        CFTimeInterval timeSincePause =
            [m_pCustomImageBtnObj.layer convertTime:CACurrentMediaTime()
                                          fromLayer:nil] - pausedTime;

        m_pCustomImageBtnObj.layer.beginTime = timeSincePause;
    }
}
4

1 回答 1

2

我认为您必须检查动画在当前事件循环期间是否已经恢复,或者是否确实发生了暂停。将此条件添加到您的resumeAnimation

- (void)resumeAnimation
{
    if (m_pCustomImageBtnObj != nil)
    {
        CFTimeInterval pausedTime = [m_pCustomImageBtnObj.layer timeOffset];
        if (pausedTime != 0) { // check if resume is called before.
            m_pCustomImageBtnObj.layer.speed = 1.0;
            m_pCustomImageBtnObj.layer.timeOffset = 0.0;
            m_pCustomImageBtnObj.layer.beginTime = 0.0;

            CFTimeInterval timeSincePause =
            [m_pCustomImageBtnObj.layer convertTime:CACurrentMediaTime()
                                          fromLayer:nil] - pausedTime;

            m_pCustomImageBtnObj.layer.beginTime = timeSincePause;
        }
    }
}
于 2013-05-27T10:44:45.537 回答