WatchKit Image Animation wait for finish

╄→гoц情女王★ 提交于 2019-12-11 12:03:44

问题


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)

回答1:


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.




回答2:


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
    }



回答3:


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


来源:https://stackoverflow.com/questions/29498214/watchkit-image-animation-wait-for-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!