WatchKit 图像动画等待完成

WatchKit Image Animation wait for finish

我目前正在寻找一种方法来等待完成我的图像动画,然后在完成后开始下一个。

我想使用完成处理程序,但是 "does not work for me" 有没有办法在这种情况下使用它?

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)

您可以使用完成处理程序,这很简单:

  • 在任何地方声明你的函数:

    func myFunc( completion:() -> ())
    {
        // Your code here
        self.GroupIMG.setBackgroundImageNamed("single")
        self.GroupIMG.startAnimatingWithImagesInRange(NSRange(location: 0, length: 300), duration: Repeater, repeatCount: self.X)
    
     completion()
    }
    
  • 在您的代码中,调用函数:

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

您必须创建一个与动画持续时间相同的计时器,并等待计时器触发以了解 WatchKit 动画何时结束。

以下是 Apple 在 Apple 开发者论坛上对这个问题的回应。

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.

您可以在此处阅读完整的话题 https://devforums.apple.com/message/1087798#1087798。基本上使用定时器并不完美,但它是你拥有的最好的。

问题已解决,但这是我的解决方案,每个人都可以使用并从中受益。 :) 您不必创建任何计时器。只需用您要执行的代码(animation/completion 处理)创建一个简单的队列即可。我的小框架会将所有东西链接在一起并执行 and/or 等待直到你想要它。

这是一个简短的示例(与 GitHub 上的相同)。我将使用更详细的示例更新 GitHub 页面。

/* 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 - 框架是用 Swift 编写的。

现在您可以为 WatchKit 应用程序创建复杂的动态动画。 请随时给我一些反馈。 :)

您的代码如下所示。还有带有完成处理的单个时间轴。

更新:

    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
    }