3

While Apple stated that animations should not be possible in Widgets, some apps managed to implement them. Example: https://apps.apple.com/app/id1588400483

These don’t only animate every second statically, but seemingly with 60/120 fps. How to achieve this?

I have been trying with the stock animation (which the documentation states don’t work) and by using timers. Both with no success at all.

thedp
  • 8,350
  • 16
  • 53
  • 95
Jann Thomas
  • 122
  • 6
  • 2
    The question is very clear, and deserves our attention. While in WWDC 2020 Apple claimed that Widgets are not supporting animation: https://stackoverflow.com/questions/64501754/animation-in-ios-14-widget Lottie seems to be able to do so. How? Good question. Probably with non standard animation approach. – thedp Nov 20 '22 at 10:26
  • 1
    Yeah I saw the other question before, should have mentioned it. But the answer is not satisfying since it is obviously possible. The question is also if they use some private API or if they have actually found a valid workaround. – Jann Thomas Nov 20 '22 at 10:30
  • 2
    Out of curiosity I would start to see if Lottie actually does work smoothly. Also, if you find a way, please post an answer. thank you. – thedp Nov 20 '22 at 10:32
  • 2
    I will definitely do so! The app I posted works completely smoothly! So I will continue to investigate and hope someone else will answer. – Jann Thomas Nov 20 '22 at 10:43
  • 2
    do you have a free example app by chance? the non premium Widgets in the example (and LottieFiles) are not animated – Casey Nov 27 '22 at 15:45
  • 2
    The first free widget in the example is animated on the latest public iOS version. – Jann Thomas Nov 28 '22 at 16:03
  • @JannThomas any progress? The bounty ends in 12 hours :) – thedp Nov 30 '22 at 07:48
  • 1
    Anyone got an answer? – erotsppa Jul 01 '23 at 23:47
  • Is it possible to display Lottie animations on a widgetkit? – mars Jul 11 '23 at 04:50

1 Answers1

1

Check this article - https://www.tridhyatech.com/blog/make-animated-live-widgets-in-the-iphone-lock-screen-using-swiftui

The logic is in updating following two methods -

func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date())
        WidgetCenter.shared.reloadAllTimelines()
        completion(entry)
    }



func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        var entries: [SimpleEntry] = []
        let currentDate = Date()
        for i in 0 ..< 60 {
            let entryDate = Calendar.current.date(byAdding: .second, value: i, to: currentDate)!
            let entry = SimpleEntry(date: entryDate)
            entries.append(entry)
        }
        let reloadDate = Calendar.current.date(byAdding: .minute, value: 1, to: currentDate)!
        let timeline = Timeline(entries: entries, policy: .after(reloadDate))
        completion(timeline)
    }

It shows how to animate an image. This logic works for 60 seconds too. I have tested this in device. But don't know yet if it will be approved by apple.

Sahil Garg
  • 11
  • 1
  • This should work, and while Apple says it shouldn't be done due to performance, I'm not sure this breaks any rules. Also, I don't think Apple actually tests such things. – thedp Aug 27 '23 at 21:47