iosswiftanimationwatchkitwkinterfaceimage

Animating Image in Watch App doesn't work (Swift)


I have a problem with my animated image.

In my page, I have a label in the center, initialised with a text "Start dictation" and an Image at the bottom initialised without image

There is my code :

func dictation() {
        let seconds = 1.0
        let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            self.label.setText("")
            self.myImage.setImageNamed("frame-")
            self.myImage.startAnimatingWithImagesInRange(NSMakeRange(0, 15), duration: 0.5, repeatCount: 0)
        })
        presentTextInputControllerWithSuggestions([], allowedInputMode: .Plain, completion: { (selectedAnswers) -> Void in
            if ((selectedAnswers != nil) && (selectedAnswers!.count>0) ){
                if selectedAnswers![0] is String {
                    self.myImage.stopAnimating()
                    self.myImage.setImageNamed("")
                    self.label.setText((selectedAnswers![0] as! String))
                }
            }
        })
}

When my dictation is finished, there is a time before the displaying of my text. So, I tried to add animation to see that it's in progress.

Here, I want to start my dictation, start in background my animation and clear my text. And, when my speech is ready to be display, I want to stop and clear the animation and print my text.

My problem is : sometimes, when I come back on my page after dictation, I found my first text "Start dictation" and not my animation. I tried with debug mode and I added breakpoints and logs in all my code. All is executed in the good order but the result is really random..

I saw also that my animation doesn't stop when I use stopAnimating() and doesn't clear when I use setImageNamed("").

Could you help me ?


Solution

  • When I started animation, I wasn't on the main page so, the code was executed but "self" was not my main page.

    To solve this, I just call my animation in the willActivate function when I come back on the main page