WatchKit Image Animation等待完成

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

我想用一个完成处理程序,但它“不适合我”是否有办法在这种情况下使用它?

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) 

您必须创build一个与animation持续时间相同的计时器,并等待计时器触发,以了解WatchKitanimation何时完成。

这是苹果在苹果开发论坛上对这个问题的回应。

目前还没有方法可以知道animation何时结束,因为我们还没有公开WatchKit API中的完成处理程序。 如果您希望看到添加的内容,请在http://bugreport.apple.com上提出增强请求。 过去,你可以使用所描述的方法,只是要知道,在任何情况下,它可能无法正常工作。 你可以select改变你的用户界面,如果你需要它永远是现货。

你可以在这里阅读完整的线程https://devforums.apple.com/message/1087798#1087798 。 基本上使用计时器是不完美的,但它是最好的。

问题解决了,但这里是我的解决scheme,每个人都可以使用并从中受益。 :)你不必创build任何计时器。 只要创build一个你想要执行的代码(animation/完成处理)的简单队列,你就完成了。 我的小框架将把所有东西链接在一起,执行和/或等到你想要的时候。

这里是一个简短的例子(与GitHub相同)。 我将更新GitHubs页面更详细的例子。

 /* 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 – 框架ist写在Swift中。

现在,您可以为您的WatchKit应用程序创build复杂dynamic的animation。 随意给我一些反馈。 🙂

这是你的代码的样子。 还有单一的时间轴与完成处理。

更新:

  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 } 

你可以使用完成处理程序,这很容易:

  • 在任何地方声明你的function:

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