2

I can't seem to figure out how to programmatically set a new image, via the outlet, and make it start animating.

Sequence

  • zeroEntering0.png
  • zeroEntering1.png
  • zeroEntering2.png
  • zeroEntering3.png
  • zeroEntering4.png

I imported the sequence of images into the Image.xcassets inside the WatchKit App

enter image description here

I can set the image in the interface builder to "zeroEntering" and set animating to "Yes" and it works correctly.

enter image description here

However, I want something more dynamic, I need a button press to choose a new animation sequence and start it off. If I try and set the image programmatically using the same name from the interface builder, the UIImage is nil.

enter image description here

What naming convention should I use when programmatically setting the UIImage? "zeroEntering", "zeroEntering0", "zeroEntering.png" or "zeroEntering0.png"

I tried using the two non-nil options and the image did not animate and went black.

2 Answers 2

8

The answer is subtle and definitely got my wheels spinning for too long.

According to this beautiful article,

You should use setImageNamed(:) when the image you want to display is either cached on the watch on is in an asset catalog in the watch app’s bundle, and use setImage(:) when the image isn’t cached — this will transfer the image data to the Apple Watch over the air!

So, I kept my images in the assets catalog on the watch app, and switch to use,

[self.testImage setImageNamed:@"zeroEntering"];
[self.testImage startAnimatingWithImagesInRange:NSMakeRange(0, 4) duration:0.2 repeatCount:100];
0

Set the image as [UIImage imageNamed:@"entering"] then call startAnimatingWithImagesInRange:duration:repeatCount:

Check it out here: https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceImage_class/#//apple_ref/occ/instm/WKInterfaceImage/startAnimatingWithImagesInRange:duration:repeatCount:

Make sure to follow the tips here: https://developer.apple.com/watchkit/tips/

4
  • I'm aware of startAnimatingWithImagesInRange, sorry forgot the mention that explicitly. However, you really just suggested what I've already tried... Where did put the images, the WatchKit Extension or the WatchKitApp? Whats weird is I can programmatically pull out a single image when they are in the extension only. But I can pull animations & individual images out when they are in the App.
    – anders
    Commented Apr 16, 2015 at 17:18
  • Hmm. Apologies, I read your question a bit quickly. If you don't need to modify the images you should definitely put them in the Watch app bundle. That way you don't need to transfer them over BTLE. Then refer to them by name as needed?
    – bgilham
    Commented Apr 16, 2015 at 17:21
  • 1
    Found this in the article you mentioned, "It is recommended that you place image resource files in the bundle of your WatchKit app (not in your WatchKit extension’s bundle). Placing them in the WatchKit app bundle lets you use the setImageNamed: method of this class to specify the animated image." I'll check back if I'm just naming things wrong or something :/. Thanks
    – anders
    Commented Apr 16, 2015 at 17:22
  • I've updated my question, any suggestions? Appreciate it
    – anders
    Commented Apr 16, 2015 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.