Giter Club home page Giter Club logo

simple-haptics's Introduction

Simple Haptics

Simple Haptics is a convenience wrapper for Core Haptics to make it easier to fire transient haptic taps, similar to UIFeedbackGenerator.

Usage

import SimpleHaptics

let haptics = SimpleHapticGenerator()

try? haptics.fire(intensity: 0.5, sharpness: 1)

Integration with SwiftUI

Simple Haptics works well when integrated with @Environment. You can create a single SimpleHapticGenerator and pass it as an environment object to your root SwiftUI view, then use it in any child views that need haptic feedback. This prevents needing to initialize lots of CHHapticEngines unecessarily, and allows you to stop and start the haptic engine on app lifecycle events. For example, in your SceneDelegate:

import SimpleHaptics
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    let haptics = SimpleHapticGenerator()

    ...

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView()
            .environmentObject(haptics) // Pass your haptic generator into your root View.

    ...

    // Be sure to stop the haptic generator when entering the background.
    func sceneDidEnterBackground(_ scene: UIScene) {
        haptics.stop()
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        try? haptics.start()
    }

}

And then child views can use it simply by creating an @EnvironmentObject variable of the correct type:

import SimpleHaptics
import SwiftUI

struct MyView: View {

    @EnvironmentObject private var haptics: SimpleHapticGenerator

    ...

}

Installation

SwiftPM

dependencies: [
    .package(url: "https://github.com/hallee/simple-haptics", from: "0.0.2")
],
targets: [
    .target(name: "YourTarget", dependencies: ["SimpleHaptics"])
]

simple-haptics's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.