Coder Social home page Coder Social logo

swiftui-haptics's Introduction

SwiftUI-Haptics

Haptics has a set of super-light modifiers and functions that adds haptic feedbacks to any SwiftUI View, for example, when the specific property changes or equals to a specific value, the feedback will be triggered automatically.

import SwiftUI
import Haptics


YourView()
    .hapticFeedback(.selection, trigger: isSelected)

or using the function programmatically

HapticGenerator.performFeedback(.selection)

Platforms

  • iOS 14.0+
  • watchOS 7.0+
  • macOS 11.0+

Haptic Feedbacks

  • start: Indicates that an activity started. (watchOS only)
  • stop: Indicates that an activity stopped. (watchOS only)
  • alignment: Indicates the alignment of a dragged item. (macOS only)
  • decrease: Indicates that an important value decreased below a significant threshold. (watchOS only)
  • increase: Indicates that an important value increased above a significant threshold. (watchOS only)
  • levelChange: Indicates movement between discrete levels of pressure. (macOS only)
  • selection: Indicates that a UI element’s values are changing. (iOS & watchOS)
  • success: Indicates that a task or action has completed. (iOS & watchOS)
  • warning: Indicates that a task or action has produced a warning of some kind. (iOS & watchOS)
  • error: Indicates that an error has occurred. (iOS & watchOS)
  • impact: Provides a physical metaphor you can use to complement a visual experience. (iOS & watchOS)

Value-based Haptic Feedbacks

Play haptic feedbacks when the value changes.

YourView()
    .hapicFeedback(.selection, trigger: isSelected)

Dynamic Haptic Feedbacks

If the value being monitored changes, returns a HapticFeedback to be performed.

Return nil means DO NOT perform any haptics.

You can provide different haptic feedbacks based on your trigger value.

YourView()
    .hapicFeedback(trigger: workStatus) { _, newValue in
        return switch {
        case .success: .success
        case .failure: .error
        default: nil
        }
    }
    .hapicFeedback(.impact, trigger: cameraSession.capturedPhoto) { _, newValue in
        return newValue == true // Only plays feedback when photo has been taken
    }

Looks familiar?

Yeah.

If you want to use .sensoryFeedback API but need to support older platform, SwiftUI-Haptics is a better solution.

Replace sensoryFeedback to hapticFeedback.

Everything just works.

Swift Package Manager

In your Package.swift Swift Package Manager manifest, add the following dependency to your dependencies argument:

.package(url: "https://github.com/LiYanan2004/SwiftUI-Haptics.git", .branch("main")),

Add the dependency to any targets you've declared in your manifest:

.target(name: "MyTarget", dependencies: ["Haptics"]),

swiftui-haptics's People

Contributors

liyanan2004 avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

volodymyr-13

swiftui-haptics's Issues

Option On/Off for settings

Hey,

In my app there will be a Settings - haptics, so user can disable haptics on my app.
It would be cool if this package had such functionality.

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.