Skip to content

CompactSlider is a SwiftUI control for macOS, iOS, watchOS and visionOS.

License

Notifications You must be signed in to change notification settings

buh/CompactSlider

Repository files navigation

CompactSlider

CompactSlider is a highly customizable multi-purpose slider control for SwiftUI. It can be used to select a single value, or a range of values, or multiple values, or a point in a grid, or a polar point in a circular grid. The slider can be displayed horizontally, vertically, or in a (circular) grid. The slider can be customized with a variety of styles and options, including the possibility to design your own style.

The slider is a replacement for the build-in slider and is designed specifically for SwiftUI. For me, the main motivation for writing a component that already exists is the very slow performance under macOS 12 and below (e.g. when you need to resize the screen with multiple sliders or when animating) and the severely outdated design at that time.

I was inspired by the slider design that Apple's Photos app developed, which makes heavy use of sliders. Also new sliders in the Camera app on iPhone 16 Pro.

Slider Types

The slider basically defines two variants: a linear slider and a grid slider. The linear slider can be horizontal or vertical, and also scrollable, where the handle is still and the scale is moving.

The grid slider can be used to select a point in a grid or a polar point in a circular grid. The point in the grid represents a value of the x and y axis. The polar point represents a value of the angle and radius.

Possible slider types defined by the CompactSliderType:

  • a horizontal slider with alignments: leading, center, trailing.
  • a vertical slider with alignments: top, center, bottom.
  • a scrollable horizontal slider.
  • a scrollable vertical slider.
  • a grid slider.
  • a circular grid slider.

Requirements

  • Swift 5.9+
  • Xcode 16+ (Use xcode15 branch, if you have to use Xcode 15)
  • SwiftUI 3+
  • macOS 12+
  • iOS 15+
  • watchOS 8+
  • visionOS 1+

Installation

  1. In Xcode go to FileAdd Packages....
  2. Search for the link below and click Add Package:
https://github.com/buh/CompactSlider.git
  1. Select to which target you want to add it and select Add Package.

Quick Start

Basic Usage

import CompactSlider

// Single value
@State private var volume: Double = 0.5

CompactSlider(value: $volume)
    .frame(height: 44)
// With range and steps
@State private var brightness: Double = 50

CompactSlider(value: $brightness, in: 0...100, step: 5)
    .frame(height: 44)
// Range selection
@State private var minPrice: Double = 20
@State private var maxPrice: Double = 80

CompactSlider(from: $minPrice, to: $maxPrice, in: 0...100)
    .frame(height: 44)
// Multiple values
@State private var markers: [Double] = [0.2, 0.5, 0.8]

CompactSlider(values: $markers)
    .frame(height: 44)

Slider Types

// Horizontal alignments
CompactSlider(value: $value)
    .compactSliderStyle(default: .horizontal(.leading))  // Default
    .compactSliderStyle(default: .horizontal(.center))
    .compactSliderStyle(default: .horizontal(.trailing))

// Vertical alignments
CompactSlider(value: $value)
    .compactSliderStyle(default: .vertical(.top))
    .compactSliderStyle(default: .vertical(.center))
    .compactSliderStyle(default: .vertical(.bottom))
    .frame(width: 44)

// Scrollable (fixed handle, moving scale)
CompactSlider(value: $value)
    .compactSliderStyle(default: .scrollable())
    .frame(height: 44)

// Grid (2D point selection)
@State private var point = CGPoint(x: 50, y: 50)

CompactSlider(point: $point, in: .zero...CGPoint(x: 100, y: 100))
    .compactSliderStyle(default: .grid())
    .frame(width: 200, height: 200)

// Circular grid (polar coordinates)
@State private var polarPoint = CompactSliderPolarPoint(angle: .zero, normalizedRadius: 0.5)

CompactSlider(polarPoint: $polarPoint)
    .compactSliderStyle(default: .circularGrid())
    .frame(width: 200, height: 200)

Common Options

CompactSlider(value: $value)
    .compactSliderOptionsByAdding(
        .tapToSlide,              // Click to set value
        .scrollWheel,             // macOS scroll wheel support
        .snapToSteps,             // Snap while dragging
        .precisionControl()       // Drag perpendicular for fine control
    )

Customization

// Colors and styling
CompactSlider(value: $value)
    .accentColor(.purple)
    .frame(height: 44)

// Custom scale
CompactSlider(value: $value)
    .compactSliderScale(
        visibility: .always,
        lineLength: 8,
        color: .blue
    )

// Custom handle
CompactSlider(value: $value)
    .compactSliderHandleStyle(
        .circle(visibility: .always, radius: 20)
    )

SystemSlider

For a native system-styled slider:

import CompactSlider

SystemSlider(value: $volume)
SystemSlider(from: $min, to: $max)

Documentation

Full API documentation available at:

The default style composition

image

Demo

The demo package contains several use cases.

Demo

macOS iOS
macOS iOS
macOS iOS
macOS iOS

GridDemo

macOS iOS
macOS iOS

CircularGridDemo

macOS iOS
macOS iOS

SystemDemo

macOS iOS
macOS iOS

visionOS visionOS

WatchOSDemo

Horizontal Vertical Grid Circular Grid
WatchOSDemo WatchOSDemo WatchOSDemo WatchOSDemo

Version 1.0 (deprecated)

Version 1.0 is deprecated and no longer supported. You can find the documentation for version 1.0 in the file: README v1.

Support

You can buy me a coffee here ☕️

License

CompactSlider is available under the MIT license

About

CompactSlider is a SwiftUI control for macOS, iOS, watchOS and visionOS.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors 4

  •  
  •  
  •  
  •  

Languages