Skip to content

DAWNCR0W/SwiftMicroInteractions

Repository files navigation

SwiftMicroInteractions

A delightful Swift library for adding micro-interactions to your iOS apps with haptic feedback, sounds, and animations.

Swift Platform SPM Compatible License

Features

  • 🎯 Easy Integration - Simple API with minimal setup required
  • 📱 Haptic Feedback - Support for all iOS haptic types including custom patterns
  • 🔊 Sound Effects - Built-in sounds with fallback to system sounds
  • Animations - Beautiful animations including bounce, shake, pulse, and more
  • 🎨 Themes - Pre-built themes and full customization support
  • ♿️ Accessibility - Respects system settings and VoiceOver
  • 🔋 Battery Conscious - Adaptive feedback and battery-saving mode
  • 📊 Analytics Ready - Built-in analytics handler support
  • 🐛 Debug Mode - Visual indicators for development

Installation

Swift Package Manager (Recommended)

Add SwiftMicroInteractions to your project through Xcode:

  1. File → Add Package Dependencies
  2. Enter: https://github.com/dawncr0w/SwiftMicroInteractions
  3. Click Add Package

Or add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/dawncr0w/SwiftMicroInteractions", from: "1.0.1")
]

CocoaPods

Coming soon - For now, please use Swift Package Manager

Carthage

Coming soon - For now, please use Swift Package Manager

Note: Version 1.0.1 includes improved compatibility for non-SPM integration methods

Quick Start

Basic Setup

import SwiftMicroInteractions

// Basic configuration (optional - has sensible defaults)
SwiftMicroInteractions.configure { config in
    config.isHapticEnabled = true
    config.isSoundEnabled = true
    config.defaultIntensity = 0.7
}

UIKit Usage

// Simple tap interaction
button.addMicroInteraction(.tap)

// With completion
button.addMicroInteraction(.success) {
    print("Success animation completed!")
}

// Advanced configuration
button.addMicroInteraction(.tap)
    .withDelay(0.1)
    .withIntensity(0.8)
    .withCondition { return self.isEnabled }
    .withCompletion { print("Tap completed") }

// Programmatic trigger
view.triggerMicroInteraction(.success)

SwiftUI Usage

// Basic usage
Button("Save") {
    // Action
}
.microInteraction(.tap)

// Toggle with interaction
Toggle("Enable Feature", isOn: $isEnabled)
    .microInteraction(.toggle)

// Custom trigger
Text("Hello")
    .microInteraction(.pulse, trigger: .onAppear)

// Manual trigger with binding
@State private var showSuccess = false

Button("Submit") {
    // Perform action
    showSuccess = true
}
.microInteraction(.success, trigger: .manual)

Available Interactions

Pre-defined Interactions

  • Basic: tap, longPress, swipe
  • States: success, failure, warning, loading
  • Actions: toggle, refresh, delete, favorite, share

Custom Interactions

// Create custom interaction
let customInteraction = MicroInteraction.custom(
    name: "powerUp",
    feedback: .combined([
        .haptic(.heavy),
        .sound(.custom(fileName: "powerup")),
        .animation(.elastic)
    ])
)

view.addMicroInteraction(customInteraction)

Themes

Built-in Themes

// Apply pre-built themes
SwiftMicroInteractions.applyTheme(DefaultTheme())
SwiftMicroInteractions.applyTheme(SubtleTheme())
SwiftMicroInteractions.applyTheme(EnergeticTheme())

Custom Theme

struct MyCustomTheme: MicroInteractionTheme {
    var tapFeedback: FeedbackType {
        .combined([.haptic(.medium), .sound(.pop)])
    }
    
    var successColor: UIColor { .systemGreen }
    var animationDuration: TimeInterval { 0.35 }
    // ... implement other properties
}

SwiftMicroInteractions.applyTheme(MyCustomTheme())

Configuration Presets

// Quick presets for common scenarios
SwiftMicroInteractions.Presets.applySubtle()      // Minimal feedback
SwiftMicroInteractions.Presets.applyEnergetic()   // Strong feedback
SwiftMicroInteractions.Presets.applySilent()      // No sounds
SwiftMicroInteractions.Presets.applyAccessible()  // Accessibility-friendly
SwiftMicroInteractions.Presets.applyBatterySaving() // Reduced battery usage

Advanced Features

Analytics Integration

SwiftMicroInteractions.setAnalyticsHandler { interaction in
    Analytics.track("micro_interaction", properties: [
        "type": interaction.name,
        "timestamp": Date()
    ])
}

Debug Mode

// Enable visual indicators for all interactions
SwiftMicroInteractions.enableDebugMode(true)

Adaptive Feedback

The library automatically reduces feedback intensity for frequently used interactions to prevent user fatigue.

// Enable/disable adaptive feedback
SwiftMicroInteractions.configure { config in
    config.isAdaptiveFeedbackEnabled = true
}

Custom Sounds

// Preload custom sounds
SwiftMicroInteractions.preloadSound(fileName: "myCustomSound")

// Use in interaction
.sound(.custom(fileName: "myCustomSound"))

Control-Specific Extensions

UIButton

button.addTapInteraction()
button.showSuccess()
button.showFailure()

UISwitch

switch.addToggleInteraction()

UISlider

slider.addSlideInteraction()

UITextField

textField.addEditingInteraction()

UIRefreshControl

refreshControl.addRefreshInteraction()

Best Practices

  1. Use Sparingly - Micro-interactions should enhance, not overwhelm
  2. Be Consistent - Use the same interactions for similar actions
  3. Respect User Preferences - Always respect system accessibility settings
  4. Test on Device - Haptics don't work in simulator
  5. Consider Context - Reduce feedback in quiet environments

Example App

Check out the included example app for comprehensive demonstrations:

cd Example/ExampleApp
open ExampleApp.xcodeproj

Requirements

  • iOS 13.0+
  • Swift 5.7+
  • Xcode 14.0+

Sound Resources

SwiftMicroInteractions automatically uses iOS system sounds, so no additional sound files are required. The library maps interactions to appropriate system sounds:

  • tap/click → Keyboard tap sound
  • pop → SMS received sound
  • success → Begin recording sound
  • error/failure → SMS sent sound
  • warning → Keyboard delete sound
  • whoosh → Mail sent sound

Custom Sounds (Optional)

You can add your own sound files by including .caf files in your app bundle:

  • Place sound files in your app's Resources folder
  • Use the same names as above (e.g., tap.caf, success.caf)
  • The library will automatically use your custom sounds when available

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

License

SwiftMicroInteractions is released under the MIT license. See LICENSE for details.

Acknowledgments

  • Inspired by the micro-interactions concept from Dan Saffer
  • Built with love for the iOS community

Changelog

Version 1.0.1

  • 🐛 Fixed Bundle.module compatibility for non-SPM integrations (CocoaPods, Carthage)
  • 🔒 Improved thread safety with main thread assertions
  • 🛡️ Enhanced associated object key safety
  • 🎭 Fixed SwiftUI onDisappear animation handling
  • 🧪 Added comprehensive test coverage for all components
  • 📦 Better support for various integration methods

Version 1.0.0

  • 🎉 Initial release with full feature set

Project Status

  • ✅ Core functionality implemented and tested
  • ✅ UIKit and SwiftUI support
  • ✅ Comprehensive test suite with edge case coverage
  • ✅ Example app included
  • ✅ Documentation complete
  • ✅ Thread-safe and production ready
  • ✅ Compatible with SPM, CocoaPods, and Carthage

Made with ❤️ by the SwiftMicroInteractions Contributors

About

easy-to-use micro-interactions combining haptic feedback, sounds, and animations for iOS apps.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages