简体   繁体   English

animateWithDuration动画一次但不两次

[英]animateWithDuration Animates Once But Not Twice

I have this piece of animation in a switch statement that runs and does what it's supposed to. 我在switch语句中运行了该动画,并执行了预期的操作。 The animation is called successfully every twenty seconds inside a timer. 在计时器内每20秒成功调用一次动画。 However, the animation does not happen any time but the first time. 但是,动画不会在任何时候发生,而是第一次。 In my code below you will see println statements that I used to try and highlight the problem. 在下面的代码中,您将看到我用来尝试突出问题的println语句。 Each println statement prints but no animation happens. 每个println语句都会打印,但不会发生动画。 What is it that I'm missing? 我想念的是什么?

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

            })

    })

}

UPDATE: If I remove image.hidden from the second completion block it works fine and animates repeatedly. 更新:如果我从第二个完成块中删除image.hidden ,它可以正常工作并反复进行动画处理。 Which is interesting because if it's not hidden it should be covering other interactive content in the view, but it's not. 有趣的是,如果未隐藏它,则它应该覆盖视图中的其他交互式内容,但不是。 The other content is perfectly accessible in simulator. 其他内容可以在模拟器中完美访问。 The UIImageView image is definitely the top layer in Storyboard. UIImageView image绝对是Storyboard中的顶层。

I could be off, but it seems like you're running from an NSTimer which may or may not be in the main thread. 我可能会离开,但似乎您正在从NSTimer中运行,它可能在主线程中,也可能不在主线程中。 UI updates need to take place in the main thread, try wrapping the completion handler in a GCD dispatch directed to the main thread like the following and see if that helps? UI更新需要在主线程中进行,尝试将完成处理程序包装在指向主线程的GCD调度中,如下所示,看看是否有帮助? Maybe even force the whole thing to the main thread. 甚至可以将整个过程强制到主线程。

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

I think you need to look for your bug elsewhere. 我认为您需要在其他地方寻找错误。 You don't seem to be missing anything in the code—as I'm able to run the animation repeatedly without any issues. 您似乎并没有在代码中遗漏任何东西,因为我能够重复运行动画而没有任何问题。 I've copied your code as-is to a separate project and the animation works every time. 我已经将您的代码原样复制到一个单独的项目中 ,并且动画每次都能正常工作。

Try replacing: 尝试更换:

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM