Isn't there an easy way to pinch to zoom in an image in Swiftui?

前端 未结 7 1514
自闭症患者
自闭症患者 2020-12-28 08:07

I want to be able to resize and move an image in SwiftUI (like if it were a map) with pinch to zoom and drag it around.

With UIKit I embedded the image into a

7条回答
  •  悲哀的现实
    2020-12-28 08:58

    Here's an alternative approach to @James and @ethoooo 's. The final zoom state and the transient gesture state are kept separate (the transient will always return 1), so it's a state you can set from a button or stepper for example in addition to the gesture itself.

      @State var scrollContentZoom: CGFloat = 1
      @GestureState var scrollContentGestureZoom: CGFloat = 1
      var contentZoom: CGFloat { scrollContentZoom*scrollContentGestureZoom }
      
      var magnification: some Gesture {
        MagnificationGesture()
          .updating($scrollContentGestureZoom) { state, gestureState, transaction in
            print("Magnifed: \(state)")
            gestureState = state
          }
          .onEnded { (state) in
            scrollContentZoom = contentZoom*state
          }
      }
    

提交回复
热议问题