-1

In my app i have widget support, And now i want to add a gif on my widget. Is it possible?

If adding a gif on widget is possible, then please provide me some example or source.

James Z
  • 12,209
  • 10
  • 24
  • 44
Tipu
  • 19
  • 4
  • 2
    Welcome to SO - Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Nov 16 '22 at 20:36
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 16 '22 at 20:42

1 Answers1

1

I'm not sure if I got you right, but I'll give the solution from what I know.

  1. I created a model that contains a list of gif images (10 images)
  2. I store the index of that array as a global variable and increment it every 1 second.
  3. I reload the widget every 1 second

Below is an example

func getTimeline(for configuration: GifCartoonIntent, in context: Context, completion: @escaping (Timeline<GifCartoonEntry>) -> ()) {
    
    var gifModel: GIFModel!
    if let selected = GlobalData.shared.gifCartoonModels.first(where: {$0.image == configuration.models?.identifier}) {
        gifModel = selected
    } else {
        gifModel = model
    }
    
    let index = getGIFIndex(gifModel.total)
    let entry = GifCartoonEntry(date: Date(), model: gifModel, index: index)
    
    let timeline = Timeline(entries: [entry], policy: .never)
    DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
        completion(timeline)
        WidgetCenter.shared.reloadTimelines(ofKind: WidgetKindType.gifCartoonWidget.rawValue)
    })
}
Minh Tường
  • 379
  • 3
  • 8