简体   繁体   English

苹果手表 - 第一次慢图像动画

[英]apple watch - slow image animation first time

I'm building a small app for apple watch. 我正在为苹果手表构建一个小应用程序。 I have a Group and a Label inside of it. 我里面有一个Group和一个Label。 What I'm trying to do is: 我想要做的是:

  • animate background image of the group 动画组的背景图像
  • fade in label after image animation ends 图像动画结束后淡入标签

My code looks essentially like this: 我的代码看起来基本上像这样:

        group.setBackgroundImageNamed("show_back-");
        group.startAnimatingWithImagesInRange(NSMakeRange(0, 39), duration: 1.5, repeatCount: 1);
        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
            self.animateWithDuration(1) { () -> Void in
                self.label.setAlpha(1)
            };
        };

The problem is that the first time this sequence is triggered, the image animation seems to run slower than 1.5 seconds, because the label starts fading in earlier than the images stop changing. 问题是,第一次触发此序列时,图像动画的运行速度似乎慢于1.5秒,因为标签在图像停止变化之前开始淡入淡出。 If this is triggered again while the app is running, everything works as expected. 如果在应用程序运行时再次触发此操作,则一切都按预期工作。 I guess it has something to do with images preloading or something. 我想它与图像预加载或其他东西有关。

How can I make it work consistently? 我怎样才能让它始终如一? I couldn't find any sort of callback on image sequence animation end to subscribe to. 我无法在图像序列动画上找到任何类型的回调来订阅。

EDIT Another problem I've noticed: I have another case when bg is animated from a dispatch_after block, and when I leave the app by tapping the crown and return by double-tapping it, either the dispatch_after block is not triggered, or the background animation is not rendered correctly the first time it is invoked (I think the second, because I tried adding a breakpoint into the dispatch block and it triggered every time I tested). 编辑我注意到的另一个问题:我有另一个案例,当bg从dispatch_after块动画时,当我通过点击表冠离开应用程序并通过双击它返回时,或者没有触发dispatch_after块,或者背景动画在第一次调用时没有正确呈现(我想第二次,因为我尝试在调度块中添加一个断点,并且每次测试时都会触发)。 I'm running watchOS2, so maybe it is related to the beta state the OS is currently in? 我正在运行watchOS2,所以它可能与操作系统目前处于测试状态有关吗?

I ran into the same issue as you. 我遇到了和你一样的问题。

This happens because on the first time you try it, the watch takes time to load the images. 发生这种情况是因为在您第一次尝试时,手表需要时间来加载图像。 Also apple doesn't give us any 'pre load' method, so I came up with a little work around it: When my controller will be displayed: 苹果也没有给我们任何“预加载”方法,所以我想出了一些工作:当我的控制器显示时:

func willActivate()

I play the animation sequence once in a background tread, this way when my user clicks on it the images are already loaded. 我在背景中播放动画序列一次,这样当我的用户点击它时图像已经加载。

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,     0)) { [weak self] in
            if let uSelf = self {
                uSelf.statusAnimationImage.setImageNamed("my image name")
                uSelf.statusAnimationImage.startAnimatingWithImagesInRange(NSMakeRange(0, 359), duration: 0.5, repeatCount: 1)
            }
        } 

That was the best way I found to solve this problem and it works for me. 这是我找到解决这个问题的最好方法,它对我有用。

try doing 试着做

 group.setBackgroundImageNamed("show_back-");

 let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC)))

 dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
        self.animateWithDuration(1) { () -> Void in
             group.startAnimatingWithImagesInRange(NSMakeRange(0, 39), duration: 1.5, repeatCount: 1);
             self.label.setAlpha(1)
        };
    };

I'm not exactly sure what you're doing but also try doing animateWithDuration(0) or (1.5) 我不确定你在做什么,但也尝试做animateWithDuration(0)或(1.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM