SwiftUI SwiftUI Gestures and Interactions 1 — Questions and Answers
Question 1: Which SwiftUI modifier is used to add a tap gesture to a view?
- .onTapGesture { } (Correct answer)
- .tapGesture { }
- .addTap { }
- .gesture(TapRecognizer())
Correct answer: .onTapGesture { }
.onTapGesture { } is the SwiftUI modifier that attaches a tap gesture action to a view.
Question 2: How do you detect a double-tap gesture in SwiftUI?
- .onTapGesture(count: 2) { } (Correct answer)
- .doubleTap { }
- .onTapGesture(times: 2) { }
- .gesture(DoubleTapGesture())
Correct answer: .onTapGesture(count: 2) { }
.onTapGesture(count: 2) specifies the number of taps required before the action fires.
Question 3: Which struct represents a drag gesture in SwiftUI?
- DragGesture (Correct answer)
- PanGesture
- SwipeGesture
- MoveGesture
Correct answer: DragGesture
DragGesture is the SwiftUI struct that recognizes dragging motion and provides translation and location values.
Question 4: What property of DragGesture.Value provides the offset from the start of the drag?
- translation (Correct answer)
- offset
- delta
- distance
Correct answer: translation
The translation property of DragGesture.Value is a CGSize representing the total movement from the drag start.
Question 5: Which modifier combines a gesture with a view while letting the gesture coexist with other gestures?
- .simultaneousGesture() (Correct answer)
- .addGesture()
- .combineGesture()
- .parallelGesture()
Correct answer: .simultaneousGesture()
.simultaneousGesture() allows multiple gestures to be recognized at the same time rather than giving priority to one.
Question 6: What does the `.highPriorityGesture()` modifier do in SwiftUI?
- Gives the gesture priority over child view gestures (Correct answer)
- Makes the gesture fire faster
- Increases touch sensitivity
- Blocks parent view gestures
Correct answer: Gives the gesture priority over child view gestures
.highPriorityGesture() causes the gesture to take priority over gestures defined on child views.
Question 7: Which SwiftUI gesture recognizes a long press on a view?
- LongPressGesture (Correct answer)
- HoldGesture
- PressGesture
- TapAndHoldGesture
Correct answer: LongPressGesture
LongPressGesture is the SwiftUI gesture type that recognizes when the user presses and holds a view for a minimum duration.
Which SwiftUI modifier is used to add a tap gesture to a view?