Fabian Boulegue
Fabian Boulegue

Reputation: 6608

WatchKit Image Animation wait for finish

I currently looking for a way to wait to finish my Image Animation and then start the next one after it is finished.

I thought to use a completion handler but it "does not work for me" is there a way to use it in that case?

if X > 1 {
            self.GroupIMG.setBackgroundImageNamed("single")
            self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: 300), duration: Repeater, repeatCount: self.X)

        }



//this should start after the if is done

            self.GroupIMG.setBackgroundImageNamed("single")
            self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: leftX), duration: Repeater, repeatCount: 1)

Upvotes: 1

Views: 1228

Answers (3)

DevAndArtist
DevAndArtist

Reputation: 5149

The Question is solved but here is my solution, which everyone can use and benefit from. :) You don't have to create any timer. Just create a simple queue with your code you want to execute (animation/completion handling) and you are done. My little framework will chain everything together and execute and/or wait until you want it to.

Here is a short example (same as on GitHub). I will update GitHubs page with more detailed examples.

/* One simple example how to create a complex Timeline object. */

Timeline.with(identifier: "SomeIdentifier") { (queue) -> Void in

    queue.add(delay: 0.0, duration: 2.0, execution: {

        // some code that will take assumed 2.0 seconds

    }, completion: {

        // some code that will excutes after 'delay' + 'duration'
    })

    queue.add(delay: 0.5, duration: 1.0, execution: {

        // some code that will executes after the top block + 'delay' time
        // this code is assumed to take 1.0 seconds by the deloper 

    })
        // any code between queue adding functions will executes immediately

}.start

TimelineKit - Framework ist written in Swift.

Now you can create complex and dynamic animations for your WatchKit application. Feel free to give me some feedback. :)

Here is how your code will look like. There are also single Timeline with completion handling.

UPDATE:

    if X > 1
    {
        /* the duration is aussumed to take 'Repeater * self.X' seconds */
        Timeline.with(identifier: "MyAnimation", delay: 0.0, duration: Repeater * self.X, execution: {

            self.GroupIMG.setBackgroundImageNamed("single")
            self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: 300), duration: Repeater, repeatCount: self.X)

        }, completion: {

            self.GroupIMG.setBackgroundImageNamed("single")
            self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: leftX), duration: Repeater, repeatCount: 1)
        }).start
    }

Upvotes: 2

Stephen Johnson
Stephen Johnson

Reputation: 5121

You have to create a timer that is the same duration as the animation and wait for the timer to fire to know when the WatchKit animation has finished.

Here is Apple's response to this question on the Apple dev forums.

There is not a way to currently know when an animation has ended, as we haven't exposed a completion handler in WatchKit's API. Please file an enhancement request at http://bugreport.apple.com if you'd like to see something added. Past that, you can use the method described, just be aware that it may not work as you like under all circumstances. You may choose to alter your UI if you need need it to always be spot on.

You can read the full thread here https://devforums.apple.com/message/1087798#1087798. Basically using a timer isn't perfect, but it is the best you have.

Upvotes: 2

Masterfego
Masterfego

Reputation: 2678

you can use a completion handler, it's easy:

  • declare your function anywhere:

    func myFunc( completion:() -> ())
    {
        // Your code here
        self.GroupIMG.setBackgroundImageNamed("single")
        self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: 300), duration: Repeater, repeatCount: self.X)
    
     completion()
    }
    
  • in your code, call the function:

    myFunc()
    {
      Void in
      // Your completion code
    
      self.GroupIMG.setBackgroundImageNamed("single")
      self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0,   length: leftX), duration: Repeater, repeatCount: 1)
    }
    

Upvotes: 0

Related Questions