Niko Niko - 3 months ago 17
iOS Question

Swift - finding the size of a UIImageview after an animation is interrupted/cancelled?

I'm creating a game where the size of my UIImage is changing and the user has to press the screen at the right time to stop the animation. The end size is then compared to a fixed constant to give the user points based on how close they were. Is there a way to use the .removeAllAnimations() method and get the size at that time? I tried using view.bounds, view.frame, view.layer.frame, and view.layer.bounds and they're all wrong. Is there another method altogether that I should be using?

Answer

both UIView and UIViewController (as well as NSView and NSViewController) act as CAAnimationDelegate which has the methods:

override func animationDidStop(anim: CAAnimation, finished flag: Bool)        
override func animationDidStart(anim: CAAnimation)

So set the delegate of your animation to whichever view, controller and use those methods to handle the stop, start events.

e.g.

    let anim = CAAnimation()
    ... set animation up here
    anim.delegate = self