Gesture recognition is the process of detecting and responding to user touch interactions like dragging, pinching to zoom (magnify), and rotating on the screen.
Click to reveal answer
beginner
Which UIKit class is used to detect drag gestures?
UIPanGestureRecognizer is used to detect drag (pan) gestures where the user moves their finger across the screen.
Click to reveal answer
beginner
How does UIPinchGestureRecognizer help in gesture recognition?
UIPinchGestureRecognizer detects pinch gestures, allowing the app to recognize when the user is zooming in or out by moving two fingers closer or farther apart.
Click to reveal answer
beginner
What is the role of UIRotationGestureRecognizer?
UIRotationGestureRecognizer detects rotation gestures, letting the app know when the user rotates two fingers around a point on the screen.
Click to reveal answer
intermediate
How do you add a gesture recognizer to a UIView in Swift?
You create a gesture recognizer instance, set its target and action, then add it to the UIView using addGestureRecognizer(). For example: view.addGestureRecognizer(panGesture).
Click to reveal answer
Which gesture recognizer detects dragging motions?
AUITapGestureRecognizer
BUIPanGestureRecognizer
CUIRotationGestureRecognizer
DUIPinchGestureRecognizer
✗ Incorrect
UIPanGestureRecognizer detects dragging or panning gestures.
What does UIPinchGestureRecognizer detect?
APinch to zoom
BRotation
CTap
DSwipe
✗ Incorrect
UIPinchGestureRecognizer detects pinch gestures used for zooming in or out.
Which method adds a gesture recognizer to a view?
AaddGesture()
BsetGestureRecognizer()
CattachGesture()
DaddGestureRecognizer()
✗ Incorrect
addGestureRecognizer() is the correct method to add a gesture recognizer to a UIView.
UIRotationGestureRecognizer detects which type of gesture?
A. Only dragging works; zoom and rotation gestures are ignored.
B. The image can be dragged, zoomed, and rotated by user gestures.
C. Only zooming works; drag and rotation gestures are ignored.
D. The image is static and does not respond to gestures.
Solution
Step 1: Analyze gesture modifiers
The code adds three gestures: DragGesture updates dragAmount offset, MagnificationGesture updates scale, RotationGesture updates angle.
Step 2: Understand combined effects
The view applies scaleEffect, rotationEffect, and offset using these state variables, so all gestures affect the image interactively.
Final Answer:
The image can be dragged, zoomed, and rotated by user gestures. -> Option B
Quick Check:
All gestures update view transforms = The image can be dragged, zoomed, and rotated by user gestures. [OK]
Hint: Multiple gestures update scale, rotation, and offset simultaneously [OK]
Common Mistakes:
Assuming only one gesture works at a time
Ignoring that gestures update state variables
Thinking gestures override each other
4. Identify the error in this SwiftUI gesture code snippet:
Circle()
.gesture(DragGesture().onChanged { value in
dragAmount = value.translation
})
.gesture(MagnificationGesture().onChanged { scale = $0 })
medium
A. Only the MagnificationGesture will work; DragGesture is ignored.
B. Both gestures work correctly without conflict.
C. The code will cause a compile-time error due to multiple gestures.
D. DragGesture will override MagnificationGesture, so only drag works.
Solution
Step 1: Understand gesture modifiers stacking
Applying multiple .gesture modifiers separately causes only the last one to be active.
Step 2: Identify which gesture is active
Since MagnificationGesture is added last, it overrides DragGesture, so only magnification works.
Final Answer:
Only the MagnificationGesture will work; DragGesture is ignored. -> Option A
Quick Check:
Last .gesture overrides previous = Only the MagnificationGesture will work; DragGesture is ignored. [OK]
Hint: Multiple .gesture modifiers override; combine gestures with simultaneousGesture [OK]
Common Mistakes:
Assuming both gestures work independently
Expecting compile error for multiple gestures
Thinking first gesture always wins
5. You want to allow a user to drag, zoom, and rotate a photo simultaneously in SwiftUI. Which approach correctly combines these gestures so all work together smoothly?
hard
A. Use only one gesture recognizer that detects drag, zoom, and rotate at once.
B. Add three separate .gesture modifiers for each gesture on the view.
C. Use .gesture for DragGesture and ignore zoom and rotate gestures.
D. Use .simultaneousGesture to combine DragGesture, MagnificationGesture, and RotationGesture on the view.
Solution
Step 1: Understand gesture combination in SwiftUI
Multiple .gesture modifiers override each other, so to combine gestures, use .simultaneousGesture.
Step 2: Apply simultaneousGesture for all three
By nesting .simultaneousGesture for DragGesture, MagnificationGesture, and RotationGesture, all gestures work together smoothly.
Final Answer:
Use .simultaneousGesture to combine DragGesture, MagnificationGesture, and RotationGesture on the view. -> Option D
Quick Check:
Combine gestures with simultaneousGesture = Use .simultaneousGesture to combine DragGesture, MagnificationGesture, and RotationGesture on the view. [OK]
Hint: Combine gestures with .simultaneousGesture for multi-touch support [OK]
Common Mistakes:
Adding multiple .gesture modifiers separately
Expecting one gesture to handle all types
Ignoring zoom and rotate gestures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.