How do I add a zoomable SVG to SwiftUI?

294 Views Asked by At

I have a map that I want to put into my app, and I am currently learning SwiftUI. I want to put in an SVG where I can scroll, zoom, set initial x & y coordinates / initial zoom, max zoom, and tap on an element to open a page. I know I can do this with UIKit, but how do I do this with SwiftUI? If I am not able to do this with an SVG, how can I do this with a raster image that is interactive?

edit: here is my code:

struct ContentView: View {

    @State private var currentPosition: CGSize = .zero

    @State private var newPosition: CGSize = .zero

    @State private var scale: CGFloat = 1.0

    @State private var currentAmount: CGFloat = 0

    @State private var finalAmount: CGFloat = 1

    

    var body: some View {

        Image("SVG")

            .frame(width: 1100, height: 1100)

            .offset(x: self.currentPosition.width, y: self.currentPosition.height)

            .gesture(DragGesture()

                .onChanged { value in

                    self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

            }

                .onEnded { value in

                    self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

                    print(self.newPosition.width)

                    print(self.newPosition.height)

                    self.newPosition = self.currentPosition

                }

            )

            .scaleEffect(finalAmount + currentAmount)

                        .gesture(

                            MagnificationGesture()

                                .onChanged { amount in

                                    self.currentAmount = amount - 1

                                }

                                .onEnded { amount in

                                    self.finalAmount += self.currentAmount

                                    self.currentAmount = 0

                                }

                        )

    }

}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}
0

There are 0 best solutions below