Back to Trending
RepositorySwiftITItaly
mttfntn

mttfntn/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.

60stars
10forks
0issues
1views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: spinning

#1 Repository of the day

1
TINIX TRENDING·Topic: auto-rotation

#1 Repository of the day

1
TINIX TRENDING·Topic: fidget-spinner

#1 Repository of the day

OneFingerRotation

Swift Platforms Release Tests License Language

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

ParameterTypeDefaultDescription
totalAngleBinding<Double>?nilAngle in degrees. Cumulative when there are no limits (not wrapped to 0–360). Setting it from outside rotates the view and stops inertia.
initialAngleAngle.zeroStarting angle when no binding is given.
rangeClosedRange<Double>?nilLimits in degrees. nil means no limits.
angleSnapDouble?nilSnap step in degrees.
inertiaBooltrueKeep spinning after release.
frictionDouble0.1Fraction of speed lost every 10 ms.
velocityMultiplierDouble0.1Scales the release speed.
autoRotationSpeedDouble20Auto-rotation speed, °/s.
autoRotationEnabledBoolfalseRuns the auto-rotation. Pauses while the finger is down.
animationAnimation?nilAnimation for external changes of totalAngle.
onAngleChanged((Double) -> Void)?nilCalled 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.x2.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 and oFKnob. Limits that can be switched on and off (range). angleSnap everywhere. 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.

RepositorySwift
jordanbaird/Ice

Powerful menu bar manager for macOS

30.2k
Oct 6, 2026
RepositorySwift
CodeEditApp/CodeEdit

📝 CodeEdit App for macOS – Elevate your code editing experience. Open source, free forever.

23.1k
Oct 17, 2026
RepositoryTypeScript
morluto/rea

Reverse engineer anything with agents, from app behavior down to native binaries.

488.2k
Dec 3, 2026
RepositoryTypeScript
n8n-io/n8n

Fair-code workflow automation platform with native AI capabilities. Combine visual building with custom code, self-host or cloud, 400+ integrations.

213.0k
Dec 3, 2026
RepositoryPython
microsoft/markitdown

Python tool for converting files and office documents to Markdown.

199.6k
Dec 3, 2026
RepositoryTypeScript
microsoft/vscode

Visual Studio Code

195.9k
Nov 30, 2026
RepositoryShell
ohmyzsh/ohmyzsh

🙃 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.

191.0k
Nov 29, 2026
Repository
getify/You-Dont-Know-JS

A book series (2 published editions) on the JS language.

184.8k
Oct 31, 2026
RepositorySwift
iina/iina

The modern video player for macOS.

47.4k
Dec 1, 2026