Skip to content

notbd/SimpleHaptics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleHaptics

Simple and easy haptic generation in iOS.

Requirements

  • iOS 13.0+ / macOS Catalyst 13.1+
  • Swift 5.8+

Installation

Swift Package Manager

Add SimpleHaptics to your project using Swift Package Manager:

  1. In Xcode, select FileAdd Package Dependencies...

  2. Enter the repository URL:

    https://github.com/notbd/SimpleHaptics.git
    
  3. Choose the version or branch you want to use

  4. Click Add Package

Alternatively, add it to your Package.swift file:

dependencies: [
  .package(url: "https://github.com/notbd/SimpleHaptics.git", from: "0.2.0")
]

Usage

Import the Package

import SimpleHaptics

Basic Usage

  • generateTask(_:) is the most common way to quickly generate haptics in a synchronous context:
// in a synchronous context
SimpleHaptics.generateTask(.heavy)
  • The task can be cancelled if needed:
let hapticTask = SimpleHaptics.generateTask(.light)
// can be cancelled later if needed
hapticTask.cancel()
  • Alternatively, use generate(_:) in an asynchronous context:
// in an async context
Task {
  await SimpleHaptics.generate(.success)
}

Available Haptic Types

Selection Haptic

  • .selection - A gentle tap to indicate selection changes

Notification Haptics

  • .success - A positive confirmation feedback
  • .warning - A warning or attention-drawing feedback
  • .error - An error alert feedback

Impact Haptics (self-explanatory)

  • .light
  • .medium
  • .heavy
  • .soft
  • .rigid

Examples

  • Quickly generate Success Feedback
func saveData() {
  // ... save your data

  // generate a success feedback after saving
  SimpleHaptics.generateTask(.success)
}
  • Generate Haptic Feedback after a delay
var body: some View {
  List {
    // ... your list items
  }
  .onChange(of: loadStatus) {
    Task {
      try? await Task.sleep(nanoseconds: UInt64(0.1 * 1_000_000_000))
      await SimpleHaptics.generate(.light)
    }
  }
}

License

MIT License

About

A Swift Package for simple and easy haptics generation in iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages