This tutorial shows how to use .gesture( Magnification() ).
Hold option key to simulate Magnification while dragging Left Mouse Button.
Closure Parameter value holds the amount of magnification that is applied.
Methods onEnded() & updating() accept Trailing Closure as last Parameter so it can be declared outside Parameters List.
Content
.updating() - Resize View while magnifying (Called while magnifying. View returns to initial size at the end)
.onEnded() - Resize View when magnification ends (Called when Gesture ends. Size is increased at the end)
Combine previous examples
Method updating()
● is called while magnifying (to give us information about current position)
● accepts @GestureState (as its first parameter)
● which can be updated inside trailing Closure (its second Parameter, through Closure's in-out state parameter)
This example shows how to change size of the View as we magnify it which is done by
● adjusting frame size with
● @GestureState size
● which is updated with value inside updating() method (every update redraws View)
● through in-out state parameter that points to $size (which must be @GestureState for this to work)
When Magnification Gesture stops
● View returns to its original size
● because magnification value returns to zero
.updating()
struct ContentView: View {
@GestureState private var magnification : CGFloat = 0
var body: some View {
Circle()
.fill(Color.green)
.frame(width: 50 + magnification, height: 50 + magnification)
.gesture( MagnificationGesture()
.updating($magnification ) { (value, state, transaction) in //Trailing Closure for body Parameter
if value.isFinite { state = value } //Amount of magnification
}
)
}
}
Initial & final size Size changes as we magnify