0

I'm trying to implement a view of an image with zoom and pan.

Below is code from another question, it kinda works but I need the pan to happen for a 2 finger pan not a single finger. Is this possible?

struct Zoom5View: View { @State private var uiimage = UIImage(named: "cat")

@GestureState private var scaleState: CGFloat = 1
@GestureState private var offsetState = CGSize.zero

@State private var offset = CGSize.zero
@State private var scale: CGFloat = 1

var magnification: some Gesture {
    MagnificationGesture()
        .updating($scaleState) { currentState, gestureState, _ in
            gestureState = currentState
        }
        .onEnded { value in
            scale *= value
        }
}

var dragGesture: some Gesture {
    DragGesture()
        .updating($offsetState) { currentState, gestureState, _ in
            gestureState = currentState.translation
        }.onEnded { value in
            offset.height += value.translation.height
            offset.width += value.translation.width
        }
}

var body: some View {
    Image(uiImage: uiimage!)
        .resizable()
        .scaledToFit()
        .scaleEffect(self.scale * scaleState)
        .offset(x: offset.width + offsetState.width, y: offset.height + offsetState.height)
        .gesture(SimultaneousGesture(magnification, dragGesture))
}

}

erotsppa
  • 14,248
  • 33
  • 123
  • 181
  • I tried to find a SwiftUI only solution but could not, so I implemented a solution based on the question here: https://stackoverflow.com/questions/66137652/swiftui-two-finger-swipe-scroll-gesture – Chris Macke Apr 13 '23 at 20:21
  • @ChrisMacke where is it? I dont see your solution – erotsppa Apr 14 '23 at 01:57
  • Apologies, I'm just now seeing UIImage in your question. The link I posted is only a solution for MacOS. – Chris Macke Apr 15 '23 at 22:56

0 Answers0