iOS Development

ios – Unable to current picture above all home windows

Spread the love


I’ve a picture that I need to current above all home windows together with .sheets in SwiftUI. I’m utilizing the answer pulled from the SO thread beneath. Nevertheless when I attempt to use the view I get the error “Static technique ‘buildExpression’ requires that ‘TopImageView’ conform to ‘View'”. How can I appropriately current an Picture above all home windows in SwiftUI?

https://stackoverflow.com/a/77533196/18070009

struct ContentView: View {
    @State var showImage = false
    var physique: some View {
        TopImageView(isPresented: $showImage, picture: "https://cdn.pixabay.com/photograph/2015/04/23/22/00/tree-736885_1280.jpg")
    }
}

struct TopImageView: ViewModifier {
    @Binding var isPresented: Bool
    @State personal var hostingController: UIHostingController<TopImage>? = nil
    let picture: String

    func showImage() {
        let swiftUIView = TopImage(picture: picture)
        hostingController = UIHostingController(rootView: swiftUIView)
        hostingController?.view.backgroundColor = .clear
        hostingController?.view.body = CGRect(
            x: 0,
            y: UIScreen.essential.bounds.peak,
            width: UIScreen.essential.bounds.width,
            peak: 0)

        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let window = windowScene.home windows.first {
            window.addSubview(hostingController!.view)

            hostingController?.view.middle.x = window.middle.x

            let contentSize = hostingController!.view.sizeThatFits(CGSize(
                width: UIScreen.essential.bounds.width,
                peak: .greatestFiniteMagnitude))

            UIView.animate(withDuration: 0.5) {
                hostingController?.view.body.origin.y = window.body.peak - contentSize.peak
                hostingController?.view.body.dimension.peak = contentSize.peak
            }
        }
    }

    func dismissImage() {
        hostingController?.view.removeFromSuperview()
        hostingController = nil
    }

    func physique(content material: Content material) -> some View {
        ZStack(alignment: .backside) {
            content material
            if isPresented {
                VStack {}
                    .onAppear {
                        showImage()
                    }
                    .onChange(of: picture) { newValue in
                        dismissImage()
                        showImage()
                    }
                    .onDisappear {
                        dismissImage()
                    }
            }
        }
    }
}

struct TopImage: View {
    let picture: String
    @State var imageHeight: Double = 0.0
    @State var imageWidth: Double = 0.0
    @State var opac: Double = 1.0
    @State personal var offset: CGPoint = .zero
    @State personal var lastTranslation: CGSize = .zero

    var physique: some View {
        ZStack {
            Rectangle().foregroundColor(.black).opacity(opac).ignoresSafeArea()
            GeometryReader { geometry in
                KFImage(URL(string: picture))
                    .resizable()
                    .aspectRatio(contentMode: .match)
                    .offset(x: offset.x + (widthOrHeight(width: true) - imageWidth) / 2.0, y: offset.y + (widthOrHeight(width: false) - imageHeight) / 2.0)
                    .background(
                        GeometryReader { proxy in
                            Colour.clear
                                .onChange(of: proxy.dimension.peak) { _ in
                                    imageHeight = proxy.dimension.peak
                                    imageWidth = proxy.dimension.width
                                }

                        }
                    )
            }
        }
        .ignoresSafeArea()
    }
}

2 thoughts on “ios – Unable to current picture above all home windows

  1. Thanks I have just been looking for information about this subject for a long time and yours is the best Ive discovered till now However what in regards to the bottom line Are you certain in regards to the supply

Leave a Reply

Your email address will not be published. Required fields are marked *