Tom Lieber's Microblog

The scale doesn't have to reset at the end of a MagnificationGesture…

Examples of SwiftUI’s MagnificationGesture usually look like this:

@GestureState var scale: CGFloat = 1

var body: some View {
    SomeView()
        .gesture(MagnificationGesture()
                    .updating($transientScale) { currentState, gestureState, transaction in
                        gestureState = currentState
                    }

They’re followed by the caveat, “And by the way, the scale will reset when the gesture ends!”

But how do you make the scale not reset when the gesture ends? Apply the scale at the end of the gesture to a persistent scale state variable. This’ll do it:

@GestureState var transientScale: CGFloat = 1
@State var scale: CGFloat = 1

var body: some View {
    SomeView()
        .gesture(MagnificationGesture()
                    .updating($transientScale) { currentState, gestureState, transaction in
                        gestureState = currentState
                    }
                    .onEnded {
                        self.scale *= $0
                    })