Challenge - 5 Problems
Gesture Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you drag the view with this code?
Consider this SwiftUI code snippet that handles drag gestures on a rectangle. What will be the final position of the rectangle after dragging?
iOS Swift
struct ContentView: View {
@State private var offset = CGSize.zero
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.offset(offset)
.gesture(
DragGesture()
.onChanged { gesture in
offset = gesture.translation
}
)
}
}Attempts:
2 left
💡 Hint
Look at how the offset state is updated inside onChanged of DragGesture.
✗ Incorrect
The offset state is updated continuously with the drag translation, so the rectangle moves with the finger and stays where the drag ended.
❓ ui_behavior
intermediate2:00remaining
What is the effect of this magnification gesture code?
Given this SwiftUI code snippet, what will happen when the user pinches to zoom on the circle?
iOS Swift
struct ContentView: View {
@State private var scale: CGFloat = 1.0
var body: some View {
Circle()
.frame(width: 100, height: 100)
.scaleEffect(scale)
.gesture(
MagnificationGesture()
.onChanged { value in
scale = value
}
)
}
}Attempts:
2 left
💡 Hint
Check how scaleEffect uses the scale state updated in onChanged.
✗ Incorrect
The scale state updates continuously with the magnification gesture value, so the circle scales smoothly as the user pinches.
❓ ui_behavior
advanced2:00remaining
What is the final rotation angle after this gesture ends?
Analyze this SwiftUI code that handles rotation gestures on an image. What will be the value of rotationAngle after the user rotates the image by 90 degrees and lifts their finger?
iOS Swift
struct ContentView: View {
@State private var rotationAngle: Angle = .zero
@State private var currentRotation: Angle = .zero
var body: some View {
Image(systemName: "arrow.right.circle.fill")
.resizable()
.frame(width: 100, height: 100)
.rotationEffect(rotationAngle + currentRotation)
.gesture(
RotationGesture()
.onChanged { angle in
currentRotation = angle
}
.onEnded { angle in
rotationAngle += angle
currentRotation = .zero
}
)
}
}Attempts:
2 left
💡 Hint
Look at how rotationAngle accumulates the angle on gesture end.
✗ Incorrect
The rotationAngle accumulates the final gesture angle onEnded, so after rotating 90 degrees, rotationAngle becomes 90 degrees.
❓ lifecycle
advanced2:00remaining
Why does the drag gesture not reset the position after lifting the finger?
In this SwiftUI drag gesture code, why does the rectangle stay where it was dragged instead of snapping back to the start?
iOS Swift
struct ContentView: View {
@State private var offset = CGSize.zero
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.offset(offset)
.gesture(
DragGesture()
.onChanged { gesture in
offset = gesture.translation
}
.onEnded { _ in
// No code here
}
)
}
}Attempts:
2 left
💡 Hint
Check what happens in onEnded closure.
✗ Incorrect
Since offset is updated during dragging and onEnded does not reset it, the rectangle remains at the dragged position.
🔧 Debug
expert3:00remaining
Why does this combined gesture fail to recognize rotation?
This SwiftUI code tries to combine drag and rotation gestures on a view. Why does the rotation gesture never trigger?
iOS Swift
struct ContentView: View {
@State private var offset = CGSize.zero
@State private var rotation = Angle.zero
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.offset(offset)
.rotationEffect(rotation)
.gesture(
DragGesture()
.onChanged { gesture in
offset = gesture.translation
}
.simultaneously(with: RotationGesture()
.onChanged { angle in
rotation = angle
})
)
}
}Attempts:
2 left
💡 Hint
Check how simultaneously is used and which gesture it is called on.
✗ Incorrect
Calling simultaneously on DragGesture with RotationGesture inside causes only the drag gesture to be recognized; the rotation gesture is ignored. The correct approach is to combine gestures at the view level using .simultaneousGesture or .gesture with a combined gesture.