0
0
iOS Swiftmobile~20 mins

Gesture recognition (drag, magnify, rotate) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gesture Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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
          }
      )
  }
}
AThe rectangle does not move at all because offset is not updated.
BThe rectangle moves but snaps back to the original position after dragging ends.
CThe rectangle moves randomly and does not follow the finger.
DThe rectangle moves following the finger and stays at the last dragged position.
Attempts:
2 left
💡 Hint
Look at how the offset state is updated inside onChanged of DragGesture.
ui_behavior
intermediate
2: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
          }
      )
  }
}
AThe circle scales only once after the pinch ends, not during the gesture.
BThe circle does not scale because scaleEffect is not connected to the gesture.
CThe circle scales up or down smoothly following the pinch gesture.
DThe circle disappears when pinching because scale becomes zero.
Attempts:
2 left
💡 Hint
Check how scaleEffect uses the scale state updated in onChanged.
ui_behavior
advanced
2: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
          }
      )
  }
}
ArotationAngle will be 90 degrees (π/2 radians) after the gesture ends.
BrotationAngle will be zero because it resets after gesture ends.
CrotationAngle will be 180 degrees because it doubles the rotation.
DrotationAngle will be negative 90 degrees due to angle sign inversion.
Attempts:
2 left
💡 Hint
Look at how rotationAngle accumulates the angle on gesture end.
lifecycle
advanced
2: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
          }
      )
  }
}
ABecause the rectangle has a fixed position and cannot move.
BBecause offset is updated during onChanged and not reset in onEnded, the position stays.
CBecause the offset resets to zero automatically after the gesture ends.
DBecause the gesture is not attached properly, so offset never changes.
Attempts:
2 left
💡 Hint
Check what happens in onEnded closure.
🔧 Debug
expert
3: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
            })
      )
  }
}
ABecause simultaneously is called on DragGesture, rotation gesture is ignored; gestures must be combined differently.
BBecause rotation gesture is inside simultaneously, it triggers correctly with drag.
CBecause rotation gesture is not attached to the view, it never triggers.
DBecause offset and rotation states conflict, causing gesture failure.
Attempts:
2 left
💡 Hint
Check how simultaneously is used and which gesture it is called on.