Italymttfntn/one-finger-rotation-swiftui
A SwiftUI modifier that lets people rotate any view with one finger, with optional limits for knobs, inertia, auto-rotation and snapping.
Featured achievements
The project's latest awards
#1 Repository of the day
#1 Repository of the day
#1 Repository of the day
OneFingerRotation
A SwiftUI package that lets people rotate any view with one finger, with a single modifier:
.oFRotation(). Limits (for knobs), inertia, auto-rotation and snapping are all optional.
For maintainers: HANDOVER.md (people) and AGENTS.md (AI assistants).
Table of Content
Installation
Requires iOS 14, macOS 11 or watchOS 7 or newer. Tested with Xcode 16.0 (Swift 6.0) to
Xcode 27.2 (Swift 6.4); Package.swift declares Swift 5.6, but older Xcode versions aren't tested.
In Xcode, choose File › Add Package Dependencies… and enter
https://github.com/mttfntn/one-finger-rotation-swiftui. In a Package.swift:
.package(url: "https://github.com/mttfntn/one-finger-rotation-swiftui", from: "2.0.0")
Then import OneFingerRotation.
Usage
The framework is a single modifier, oFRotation. Limits, inertia, auto-rotation and snapping are all optional. Give the view a square frame before the modifier, so it rotates around its center:
Image("wheel")
.frame(width: 300, height: 300)
.oFRotation()
A fuller example:
@State private var angle: Double = 0
Image("wheel")
.frame(width: 300, height: 300)
.oFRotation(
totalAngle: $angle, // optional: degrees, read & write
range: nil, // optional: limits, e.g. -135...135
angleSnap: 30, // optional: rest on multiples of 30°
inertia: true,
friction: 0.1,
velocityMultiplier: 0.1,
autoRotationSpeed: 45, // degrees per second (negative = counter-clockwise)
autoRotationEnabled: isSpinning,
animation: .spring(), // used when `angle` is changed from outside
onAngleChanged: { print($0) }
)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
totalAngle | Binding<Double>? | nil | Angle in degrees. Cumulative when there are no limits (not wrapped to 0–360). Setting it from outside rotates the view and stops inertia. |
initialAngle | Angle | .zero | Starting angle when no binding is given. |
range | ClosedRange<Double>? | nil | Limits in degrees. nil means no limits. |
angleSnap | Double? | nil | Snap step in degrees. |
inertia | Bool | true | Keep spinning after release. |
friction | Double | 0.1 | Fraction of speed lost every 10 ms. |
velocityMultiplier | Double | 0.1 | Scales the release speed. |
autoRotationSpeed | Double | 20 | Auto-rotation speed, °/s. |
autoRotationEnabled | Bool | false | Runs the auto-rotation. Pauses while the finger is down. |
animation | Animation? | nil | Animation for external changes of totalAngle. |
onAngleChanged | ((Double) -> Void)? | nil | Called on every change. |
Limits (knobs)
Pass range to turn the view into a knob. The angle can't go past the limits, whether it's dragged, thrown or auto-rotating. Values set from outside are clamped too.
@State private var volumeAngle: Double = 0
@State private var limited = true
Image("knob")
.frame(width: 120, height: 120)
.oFRotation(
totalAngle: $volumeAngle,
range: limited ? -135...135 : nil, // limits can be switched on and off at runtime
angleSnap: 27, // 11 detents across 270°
animation: .easeInOut
)
Button("Mute") { volumeAngle = -135 } // external changes animate and stop inertia
To get a 0...1 value, use the included helper:
let volume = RotationMath.knobValue(forAngle: volumeAngle, minAngle: -135, maxAngle: 135)
Snapping
angleSnap works with or without inertia and limits (#2). While dragging or coasting, the view steps between detents. When it stops, it rests exactly on a multiple of the step. With limits, the steps count from the lower limit. Snapping is paused while auto-rotation runs.
Migrating from 1.x
| 1.x | 2.0 |
|---|---|
.simpleRotation(angleSnap: .constant(60)) | .oFRotation(angleSnap: 60, inertia: false) |
.simpleRotationInertia() | .oFRotation() |
.valueRotation(totalAngle: $a, onAngleChanged: { a = $0 }) | .oFRotation(totalAngle: $a, inertia: false) |
.valueRotationInertia(totalAngle: $a, ...) | .oFRotation(totalAngle: $a) |
.autoRotation(autoRotationSpeed: $s, autoRotationActive: $on) | .oFRotation(inertia: false, autoRotationSpeed: s, autoRotationEnabled: on) |
.valueAutoRotationInertia(totalAngle: $a, ...) | .oFRotation(totalAngle: $a, autoRotationEnabled: true) |
.knobRotation / .knobInertia / .oFKnob(knobValue: $v, minAngle: lo, maxAngle: hi) | .oFRotation(totalAngle: $angle, range: lo...hi). The value is now in degrees; convert with RotationMath.knobValue(forAngle:minAngle:maxAngle:). |
A binding now updates itself, so onAngleChanged: { a = $0 } isn't needed any more.
Known limits
- The view rotates around the center of its frame, so give it a square frame before the
modifier (
.frame(width: 300, height: 300)). - The whole frame reacts to touches, including the corners outside a round view.
- Touches within 4 points of the center are ignored, because the angle there is undefined.
- Changing the angle from outside always stops inertia.
- With snapping, the view moves between detents in steps; while auto-rotation runs, snapping is paused.
Testing
All the gesture, inertia, snapping and limit logic lives in RotationMath and RotationEngine, which have no UI, and is covered by unit tests (Swift Testing):
swift test
An interactive demo, OneFingerRotationDemo, is included in DEBUG builds. Open the package in Xcode and use the canvas in Live mode to try it.
Versions
- 2.0.0: Builds again with Xcode 27 (Swift 6.4), which rejected 1.2.1. One modifier,
oFRotation, replaces every 1.x modifier andoFKnob. Limits that can be switched on and off (range).angleSnapeverywhere. Frame-rate-independent inertia that follows the throw direction, with no fling after the finger rests. Touch coordinates no longer rotate with the content. Accessibility support (VoiceOver adjustable action). Unit tests. - 1.2.1: Refinements to the content of the README file, adjustments to the Simple Rotation function, video examples in the README file.
- 1.2.0: Implementation of functions of snapping for Simple Rotation, Simple Rotation with Inertia
- 1.0.0: Implementation of the modifiers: Simple Rotation, Simple Rotation with Inertia, Value Rotation, Value Rotation with Inertia, Auto Rotation, Value Auto Rotation, Value Auto Rotation with Inertia, Knob, Knob with Inertia.
Contributing
Bug reports, ideas and pull requests are welcome: see CONTRIBUTING.md. Security problems go through SECURITY.md.
License
MIT. © Matteo Fontana.
Similar Projects
Discover other similar projects and AI models.
📝 CodeEdit App for macOS – Elevate your code editing experience. Open source, free forever.
Reverse engineer anything with agents, from app behavior down to native binaries.
Fair-code workflow automation platform with native AI capabilities. Combine visual building with custom code, self-host or cloud, 400+ integrations.
Python tool for converting files and office documents to Markdown.
🙃 A delightful, community-driven framework for managing your zsh configuration. Built by 2,600+ contributors, with hundreds of plugins (git, rails, laravel, node, python, docker, kubectl, aws, and more), themes to spice up your terminal, and automatic updates to keep your command line feeling like home.