SwiftUI SwiftUI Gestures and Interactions 2 — Questions and Answers
Question 1: Which gesture type in SwiftUI is used to detect pinching motions for zooming?
- MagnificationGesture (Correct answer)
- PinchGesture
- ZoomGesture
- ScaleGesture
Correct answer: MagnificationGesture
MagnificationGesture recognizes pinch-to-zoom interactions and provides a magnification value.
Question 2: What is the purpose of the `@GestureState` property wrapper in SwiftUI?
- To store transient gesture values that reset when the gesture ends (Correct answer)
- To persist gesture data across app sessions
- To define the initial state of a gesture
- To track gesture history
Correct answer: To store transient gesture values that reset when the gesture ends
@GestureState automatically resets to its initial value when the gesture ends, making it ideal for transient interaction state.
Question 3: How do you chain two gestures so that the second gesture only starts after the first succeeds?
- firstGesture.sequenced(before: secondGesture) (Correct answer)
- firstGesture.then(secondGesture)
- firstGesture.followed(by: secondGesture)
- SequentialGesture(first, second)
Correct answer: firstGesture.sequenced(before: secondGesture)
The .sequenced(before:) method creates a gesture that requires the first gesture to complete before recognizing the second.
Question 4: Which property of RotationGesture.Value gives the angle of rotation?
- rotation (Correct answer)
- angle
- radians
- degrees
Correct answer: rotation
The rotation property of RotationGesture.Value returns an Angle struct representing how far the gesture has rotated.
Question 5: When using DragGesture, which method is called to handle ongoing movement during the drag?
- .onChanged { } (Correct answer)
- .onMove { }
- .onDrag { }
- .whileDragging { }
Correct answer: .onChanged { }
.onChanged { } is the gesture callback that fires continuously as the drag position updates.
Question 6: What does the `minimumDistance` parameter in DragGesture control?
- How far the user must move before the gesture is recognized (Correct answer)
- The maximum allowed drag length
- The velocity threshold for the gesture
- The distance from screen edge required
Correct answer: How far the user must move before the gesture is recognized
minimumDistance sets the threshold in points the user must drag before DragGesture begins recognizing the gesture.
Question 7: Which modifier allows a SwiftUI view to be dragged and dropped using the drag-and-drop API?
- .draggable() (Correct answer)
- .onDrag()
- .dragSource()
- .draggableItem()
Correct answer: .draggable()
.draggable() is the modern SwiftUI modifier (iOS 16+) that makes a view participate in drag-and-drop as a source.
Which gesture type in SwiftUI is used to detect pinching motions for zooming?