user6539552
user6539552

Reputation: 1451

Swift: Today Widget with animation

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?

Upvotes: 1

Views: 1520

Answers (1)

Alec O
Alec O

Reputation: 1707

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)

Upvotes: 1

Related Questions