Search code examples
swiftwatchkitapple-watch

Programmatically Set UIImage Animation with WatchKit in Swift


I can't seem to figure out how to programmatically animate UIImages.

I've seen a code written in Objective-C, but looking for a solution in Swift.

The closest code I can find is:

imageView.startAnimatingWithImages(in: ???[NSRange] , duration: 1, repeatCount: 100)

but I'm not sure what to put in NSRange. Images is stored in my asset folder in a folder: enter image description here

Code in swift(but not for Apple Watch): How to animate images in Swift?

Objective- C code: Programatically Set UIImage Animation with WatchKit


Solution

  • Found a swift solution: quiet simple, I just needed to add an image outlet and write the following two code items:

    class InterfaceController: WKInterfaceController {
        @IBOutlet weak var imageObject: WKInterfaceImage!
    
        override func awake(withContext context: Any?) {
            super.awake(withContext: context)
            imageObject.setImageNamed("plank")
            imageObject.startAnimating()
            // Configure interface objects here.
        }
    }