nycdanie
nycdanie

Reputation: 2941

Completion Handler or function for this style of animation

I can't find an animation completion handler/function for this kind of style of code.

I didn't use the Animation block provided that has a build in completion function once animation is complete, because I need to be in control of the repetition process.

What I need to do is if this animation is completed, the image should be removeFromSuperView(). Is there a workaround for this one?

@IBAction func buttonPressed() {
    var repeatCount = Float(10.0)
    var duration = 2.0

    //if finished, remove image using image.removeFromSuperview()

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(duration)
    UIView.setAnimationRepeatCount(repeatCount)
    image.frame = CGRectMake(160, 300, 50, 50)
    UIView.commitAnimations()
}

Upvotes: 2

Views: 472

Answers (2)

Alex Curylo
Alex Curylo

Reputation: 4772

You really, seriously, should use block-based animation; but if you must, set the delegate and callback(s) before committing the animations, declared right by the other setAnimationXXX functions.

// no getters. if called outside animation block, these setters have no effect.
class func setAnimationDelegate(delegate: AnyObject?) // default = nil
class func setAnimationWillStartSelector(selector: Selector) // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
class func setAnimationDidStopSelector(selector: Selector) // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

Also, what you're declaring here

    [UIView.beginAnimations(nil, context: nil)]

is "an array containing the result of beginAnimations, which is Void". This is not useful in any conceivable context.

Upvotes: 0

matt
matt

Reputation: 535230

You shouldn't really be using the old style beginAnimations / commitAnimations structure. But if you are going to use it, then simply give the animation a delegate with setAnimationDelegate: and specify a selector to be called when the animation ends with setAnimationDidStopSelector:, as I describe here:

http://www.apeth.com/iOSBook/ch17.html#_modifying_an_animation_block

Upvotes: 3

Related Questions