1

I'd like to write a Today Widget Extension with an UIImageView which display an animation.

Here are my code.

    override func viewDidLoad() {
        super.viewDidLoad()
        layoutComponents()
        imageView.startAnimating()
   }

func layoutComponents() {        
    self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded

    self.view.backgroundColor = UIColor.blue

    let images = [UIImage(named:"1.png"), UIImage(named:"2.png"), UIImage(named:"3.png")]

    imageView.frame = CGRect(x: 0, y: 0, width: self.extensionContext!.widgetMaximumSize(for: .expanded).width, height: self.extensionContext!.widgetMaximumSize(for: .expanded).height)
    imageView.animationImages = images
    imageView.image = images[0]
    imageView.animationDuration = 1.0
    imageView.animationRepeatCount = 0
    self.view.addSubview(imageView)

    debugPrint("Done LayoutComponents")
  }

The code compiled successfully. However, the widget cannot be loaded, it display "Unable to Load" on the widget when run on the iPhone. And in the debug print, after printing "Done LayoutComponents". It has a line "Program ended with exit code: 0".

Any clues what i have done wrong? thanks.

Note that whenever i put the code imageView.startAnimating() in either viewWillAppear() viewDidAppear or widgetPerformUpdate() it will exit and with the problem that "Unable to load".

My question is actually is Today Extension support animating an UIImageView?

user6539552
  • 1,331
  • 2
  • 19
  • 39
  • Please copy+paste the debugger's results into your question. Often times you can find a little more information on why it crashed if you scroll up. – Alec O May 10 '17 at 14:23
  • It did not crashed. It exit with exit code 0. 2017-05-10 22:34:26.597307+0800 PetWidget[9632:3946653] No active animation block! Program ended with exit code: 0 That's all printed. – user6539552 May 10 '17 at 14:34
  • Unable to load is a crash in case of widget. Also check were the images are located. – VIP-DEV May 10 '17 at 15:00
  • Yes, no problem if i display the image one by one. – user6539552 May 10 '17 at 15:02

1 Answers1

1

imageView.startAnimating() belongs in viewDidAppear instead of viewDidLoad as there is nothing to animate before the view appears.

Also verify that "User Preferred Explicit Size" is disabled in the storyboard (Today's extension iOS10 Show More/Less)

Community
  • 1
  • 1
Alec O
  • 1,697
  • 1
  • 18
  • 31
  • http://www.anhuiyouxi.com/today-s-extension-ios10-show-more-less/ Does this help? They had the same error as you: `No active animation block` – Alec O May 10 '17 at 14:46
  • Mine is always in extended mode. And change of mode won't change the result :( – user6539552 May 10 '17 at 14:49
  • http://stackoverflow.com/questions/40441773/change-ncwidgetdisplaymode-programmatically-in-ios10-widget Did you test this against iOS9? It appears that iOS10 does this differently. – Alec O May 10 '17 at 14:55
  • 1
    yes, tried iOS9 and 8 as well. the same output I wonder if Today Widget support animating an UIImageView? – user6539552 May 10 '17 at 14:59