SwiftUI make transition with document-base image brower app how?

65 Views Asked by At

I just follow some example to build my first document-base app. The UTType is image.The code is below,this do work properly,but there is not transition when you select the image item.

import SwiftUI
import UniformTypeIdentifiers

@main
struct hhhApp: App {
    //@State var tag:
   
    var body: some Scene {
        DocumentGroup(viewing: MyImageFormatDocument.self) {file in
                MyImageFormatViewer(document: file.$document)
                //.transition(.scale.animation(.easeInOut(duration: 2)))
                .onAppear{print("appear")}
        }   
    }
}

struct MyImageFormatViewer:View{
    //@State private var image: UIImage = document.image
    @State private var showDetail = false

    @Binding var document:MyImageFormatDocument
    var body: some View{
            Image(uiImage: document.image)  
    }
}


struct MyImageFormatDocument: FileDocument {
    static var readableContentTypes = [UTType.image]
    var image = UIImage()
    // create from a file
    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let decodedImage: UIImage = UIImage(data: data)
          else {
            throw CocoaError(.fileReadCorruptFile)
        }
        image = decodedImage
    }

    // write
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = image.pngData()!
        //return .init(regularFileWithContents: data)
        return FileWrapper(regularFileWithContents: data)
    }
    
    
    init(initialImage: UIImage = UIImage()) {
        image = initialImage
    }
  
    init(fileWrapper: FileWrapper, contentType: UTType) throws {
        if let data = fileWrapper.regularFileContents {
            image = UIImage(data: data) ?? UIImage()
            print("init")
        }
    }
 
    func write(to fileWrapper: inout FileWrapper, contentType: UTType) throws { }
}

I want to implement the animation just like the the "file" app of the system.When you select the image it will make transition scale from the image item from in the file browser to the image preview.

I do try to add animation or transition for the MyImageFormatViewer,but i can get the effect that i want. How can i know the size or coordinate for the image item or there is a easy way to implement this?

Thanks.

0

There are 0 best solutions below