In SwiftUI, how do you let users pinch to zoom and twist to rotate a photo at the same time with MagnifyGesture and RotateGesture?
answer
- iOS 17 gesture types
- one combined gesture value
- optional first and second
- factor since this pinch began
- multiply scale, add angle
basics
~10 sCombine them as MagnifyGesture().simultaneously(with: RotateGesture()), read value.first?.magnification and value.second?.rotation into @GestureState while the fingers move, and in onEnded multiply the committed scale and add the angle into @State. Both types need iOS 17.
solid answer
~40 s`MagnifyGesture` and `RotateGesture` arrived in iOS 17. `simultaneously(with:)` combines them into a `SimultaneousGesture`, whose value has two optionals, `first` and `second`, one per child gesture. `magnification` is the scale factor since this pinch began, starting at `1`, and `rotation` is the `Angle` turned since it began. So the view shows `scale * live.scale` and `angle + live.angle`: the live part sits in a `@GestureState` written in `updating`, and `onEnded` folds it into `@State` with `scale *= magnification` and `angle += rotation`. Skip the committed value and the photo snaps back to 1x at every new pinch. The older `MagnificationGesture` and `RotationGesture` report a bare `CGFloat` and `Angle`; the iOS 27.0 SDK marks them as renamed to the new types, with deprecation set for a future release.
code
swift · 29 linesimport SwiftUI
struct ZoomRotatePhoto: View {
struct Live { var scale: CGFloat = 1; var angle: Angle = .zero }
@State private var scale: CGFloat = 1 // committed zoom
@State private var angle: Angle = .zero // committed rotation
@GestureState private var live = Live() // this pinch only
var body: some View {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.scaleEffect(scale * live.scale)
.rotationEffect(angle + live.angle)
.gesture(
MagnifyGesture()
.simultaneously(with: RotateGesture())
.updating($live) { value, state, _ in
state.scale = value.first?.magnification ?? 1
state.angle = value.second?.rotation ?? .zero
}
.onEnded { value in
scale *= value.first?.magnification ?? 1
angle += value.second?.rotation ?? .zero
}
)
}
}go deeper
Recall the two iOS 17 types, MagnifyGesture and RotateGesture, and that simultaneously(with:) runs them together.
Explain why magnification restarts at 1 each pinch, and how committed @State and live @GestureState combine by multiplying scale and adding angle.
Handle the edges: clamping, resetting on double tap, enabling pan only when zoomed, and the iOS 16 availability check.
Decide when to migrate off MagnificationGesture: the compiler is silent today, so the rename only happens on a schedule a lead sets.
## The two gesture types A pinch and a two-finger twist each have their own gesture type. Both were introduced in **iOS 17** (macOS 14), and neither exists on tvOS or watchOS: | | `MagnifyGesture` | `RotateGesture` | |---|---|---| | Main value | `magnification: CGFloat` | `rotation: Angle` | | Also reports | `velocity`, `startAnchor`, `startLocation`, `time` | `velocity`, `startAnchor`, `startLocation`, `time` | | Initializer | `init(minimumScaleDelta: CGFloat = 0.01)` | `init(minimumAngleDelta: Angle = .degrees(1))` | | Replaces | `MagnificationGesture` (value `CGFloat`) | `RotationGesture` (value `Angle`) | The minimum delta is how much the fingers must change the scale or angle before the gesture starts reporting. `startAnchor` is a `UnitPoint` describing where in the view the gesture began, and `startLocation` is the same point as a `CGPoint`. ## Running both at once Attaching two separate `.gesture(...)` modifiers to one view does not express "both at the same time". You combine the gestures into one with **`simultaneously(with:)`**, which produces a `SimultaneousGesture`. Apple describes it as a gesture that evaluates its two child gestures at the same time, with a value that is a struct holding **two optional values**: - `value.first` is the `MagnifyGesture.Value?`, `nil` while the pinch has produced no value. - `value.second` is the `RotateGesture.Value?`, `nil` while the twist has produced no value. - Reading them with a default (`?? 1` and `?? .zero`) keeps the arithmetic simple when only one part is moving. ## Committed and live values The numbers each gesture reports are **relative to the start of the current gesture**: `magnification` starts at `1` for every new pinch, and `rotation` starts at zero. That is why a photo viewer keeps two layers of state, exactly as with a drag: 1. **Committed** zoom and angle in `@State`, which survive between gestures. 2. **Live** zoom and angle in a `@GestureState`, written in `updating` and reset by SwiftUI when the fingers lift or the gesture is cancelled. 3. The view shows the combination: `scaleEffect(scale * live.scale)` and `rotationEffect(angle + live.angle)`. 4. `onEnded` folds the live part in: `scale *= magnification` (scales **multiply**), `angle += rotation` (angles **add**). The classic bug is to drive `scaleEffect` straight from `value.magnification` with no committed value. The first pinch looks fine; the second starts at 1x again, because the new gesture's magnification restarts at `1`. ## How the values arrive The callbacks behave as they do for any gesture, and that shapes the code: - `updating` runs when SwiftUI recognises the combined gesture and whenever either value changes, so the live preview follows the fingers. - SwiftUI does not call `updating` when the gesture ends or is cancelled; it resets the `@GestureState` to `Live()` instead. - `onEnded` runs only when the gesture succeeds. If the pinch is cancelled, nothing is committed and the photo returns to its last committed zoom and angle rather than keeping a half-finished value. ## The renamed types Before iOS 17 the same interactions used `MagnificationGesture`, whose value is a bare `CGFloat`, and `RotationGesture`, whose value is a bare `Angle`. In the iOS 27.0 SDK both carry `renamed: "MagnifyGesture"` and `renamed: "RotateGesture"` with a deprecation version reserved for a future release, and Apple's documentation lists them under deprecated gestures. With that marking the Swift 6.4 compiler does not warn yet, so old code keeps building silently. The new types give you more than the rename: velocity and the start anchor are available without extra bookkeeping. An app that still deploys to iOS 16 cannot use the new types without an availability check; the compiler reports *'MagnifyGesture' is only available in iOS 17.0 or newer*. ## Practical refinements - **Clamp the zoom** in `onEnded`, for example `scale = min(max(scale * m, 1), 4)`, so a photo cannot shrink below its fitted size or grow without limit. - **Reset on double tap** by setting the committed values back to `1` and `.zero`; combining that tap with other taps is a composition question of its own. - **Pan only while zoomed.** A drag that moves a zoomed photo is usually enabled only when `scale > 1`, so it does not compete with the gallery around the photo at 1x. - **Zoom around the fingers.** `scaleEffect(_:anchor:)` accepts a `UnitPoint`; feeding it `startAnchor` zooms around where the pinch began, though keeping the image steady across several pinches also needs an offset correction.
- How do you keep the zoom between 1x and 4x?Clamp when committing: `scale = min(max(scale * m, 1), 4)` in `onEnded`. If the live preview should also stop at the limits, clamp the product `scale * live.scale` where you apply `scaleEffect`, so the photo never visibly overshoots before snapping back.
- What changes if the app still supports iOS 16?`MagnifyGesture` and `RotateGesture` are unavailable there, and the compiler says *'MagnifyGesture' is only available in iOS 17.0 or newer*. Either branch with `if #available(iOS 17, *)` or keep `MagnificationGesture` and `RotationGesture`, whose values are a bare `CGFloat` and `Angle`, until the deployment target reaches iOS 17.
- MagnifyGesture's magnification is the photo's total zoom across all pinches
- Two separate gesture modifiers on one view run a pinch and a twist together
- SimultaneousGesture's value always holds both child values, never nil
- Scales from consecutive pinches should be added, not multiplied
- MagnificationGesture now fails to compile, so every project must migrate at once
Assumes iOS 17 or later: MagnifyGesture and RotateGesture were introduced in iOS 17, and the iOS 27.0 SDK marks MagnificationGesture and RotationGesture as renamed to them with a future deprecation.