I am working on a sticker app and I am building a custom sticker app in SwiftUI. I have created a custom UIViewRepresentable
to allow a MSStickerView
to be displayed in SwiftUI. I have local *.gif
files in my project and I am loading them into the MSStickerView
successfully, however when they are loaded in my iMessage sticker extension the stickers are not animating by default. When I tap on the MSStickerView
the gif begins to animate, I'm not sure what else I can do to get this working properly in my app. Some sample code below:
public struct CustomStickerView: UIViewRepresentable {
var sticker: CustomSticker
public init(sticker: CustomSticker) {
self.sticker = sticker
}
public func makeUIView(context: Context) -> MSStickerView {
let v = MSStickerView()
if sticker.fileType == .gif {
v.startAnimating()
}
return v
}
public func updateUIView(_ uiView: MSStickerView, context: Context) {
uiView.sticker = sticker.sticker
}
}
// CustomSticker
public var sticker: MSSticker? {
guard let imagePath = Bundle.main.path(forResource: name, ofType: ".\(fileType.rawValue)") else {
print("Failed to get sticker - \(name).\(fileType.rawValue)")
return nil
}
let path = URL(fileURLWithPath: imagePath)
return try? MSSticker(contentsOfFileURL: path, localizedDescription: name)
}