简体   繁体   中英

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. The animation is called successfully every twenty seconds inside a timer. 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. Each println statement prints but no animation happens. 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. 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.

I could be off, but it seems like you're running from an NSTimer which may or may not be in the main thread. 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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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