【问题标题】:animateWithDuration Animates Once But Not TwiceanimateWithDuration 动画一次但不是两次
【发布时间】:2015-08-23 22:49:06
【问题描述】:

我在switch 语句中有这段动画,它运行并执行它应该做的事情。动画在计时器内每 20 秒成功调用一次。但是,动画不是第一次发生,而是第一次发生。在下面的代码中,您将看到 println 我用来尝试突出问题的语句。每个println 语句都会打印,但没有动画发生。我错过了什么?

func popAnimation(indexNumber: Int) {

    let image = self.overlayImageView[indexNumber]
    image.hidden = false
    image.alpha = 0.0

    UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in

        println("animation ran")

        println(image.alpha)
        image.image = UIImage(named: "lowCountOverlay")
        image.alpha = 1.0


        }, completion: { (Bool) -> Void in

            UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in

                println("animation 2 ran")

                println(image.alpha)
                image.alpha = 0.0


                }, completion: { (Bool) -> Void in

                    println("animation 3 ran")
                    image.hidden = true

            })

    })

}

更新:如果我从第二个完成块中删除image.hidden,它可以正常工作并重复动画。这很有趣,因为如果它没有隐藏,它应该覆盖视图中的其他交互式内容,但事实并非如此。其他内容在模拟器中完全可以访问。 UIImageView image 绝对是 Storyboard 的顶层。

【问题讨论】:

  • 难道不是用alpha0覆盖其他交互内容吗?

标签: swift animation animatewithduration


【解决方案1】:

我可能会离开,但您似乎是从一个 NSTimer 运行的,它可能在也可能不在主线程中。 UI 更新需要在主线程中进行,尝试将完成处理程序包装在指向主线程的 GCD 调度中,如下所示,看看是否有帮助?甚至可能将整个事情强制到主线程。

UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
    // First animation
}, , completion: { (Bool) -> Void in
    dispatch_async(dispatch_get_main_queue(),{
        // second animation
    })
})

【讨论】:

  • 刚试过这个,不,它似乎与 GCD 无关。至少不是那种格式。更新了 OP。
【解决方案2】:

我认为您需要在其他地方寻找您的错误。您似乎没有遗漏代码中的任何内容——因为我能够重复运行动画而没有任何问题。我已经按原样复制了您的代码to a separate project,并且动画每次都有效。

【讨论】:

    【解决方案3】:

    尝试替换:

    let image = self.overlayImageView[indexNumber]
    image.hidden = false
    image.alpha = 0.0
    

    let image = self.overlayImageView[indexNumber]
    image.superview.bringSubviewToFront(image)
    image.hidden = false
    image.alpha = 0.0
    

    【讨论】:

    • 但是在函数的开头image.hidden被设置为false
    • 没有骰子。这个让我难住了。它运行一次完美,但一旦计时器再次触发它,除了println之外什么都没有发生。
    • @spacemonkey 你有机会上传你的源代码吗?
    • 我很感激提供看看它,但该项目几乎完成了,~2000 行。另外,我是 github 新手。
    • @spacemonkey 你可以尝试在一个虚拟项目中重现同样的问题——你不需要分享实际的问题。在此过程中,您可能会自己识别错误;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多