在ios中暂停和恢复动画的问题

时间:2013-05-27 07:25:06

标签: ios animation cabasicanimation

我使用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;
    }
}

1 个答案:

答案 0 :(得分: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;
        }
    }
}