102 lines
2.4 KiB
Swift
102 lines
2.4 KiB
Swift
//
|
|
// HapticFeedbackGenerator.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 16/10/24.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class HapticFeedbackGenerator{
|
|
|
|
// Singleton instance for reuse across the app
|
|
static let shared = HapticFeedbackGenerator()
|
|
|
|
// Private initializer to prevent instantiation from outside
|
|
private init() {}
|
|
|
|
/*
|
|
// Trigger light impact feedback
|
|
HapticFeedbackGenerator.shared.lightImpact()
|
|
|
|
// Trigger success notification feedback
|
|
HapticFeedbackGenerator.shared.successNotification()
|
|
|
|
// Trigger selection feedback
|
|
HapticFeedbackGenerator.shared.simpleSelection()
|
|
|
|
*/
|
|
|
|
// MARK: - Impact Feedback
|
|
func impact(style: UIImpactFeedbackGenerator.FeedbackStyle) {
|
|
let generator = UIImpactFeedbackGenerator(style: style)
|
|
generator.prepare() // Prepares the feedback generator to reduce latency
|
|
generator.impactOccurred() // Trigger the haptic feedback
|
|
}
|
|
|
|
// MARK: - Notification Feedback
|
|
func notification(type: UINotificationFeedbackGenerator.FeedbackType) {
|
|
let generator = UINotificationFeedbackGenerator()
|
|
generator.prepare()
|
|
generator.notificationOccurred(type)
|
|
}
|
|
|
|
// MARK: - Selection Feedback
|
|
func selection() {
|
|
let generator = UISelectionFeedbackGenerator()
|
|
generator.prepare()
|
|
generator.selectionChanged()
|
|
}
|
|
|
|
// MARK: - Common Presets
|
|
|
|
// Light impact feedback
|
|
func lightImpact() {
|
|
impact(style: .light)
|
|
}
|
|
|
|
// Medium impact feedback
|
|
func mediumImpact() {
|
|
impact(style: .medium)
|
|
}
|
|
|
|
// Heavy impact feedback
|
|
func heavyImpact() {
|
|
impact(style: .heavy)
|
|
}
|
|
|
|
// Soft impact feedback (available iOS 13+)
|
|
func softImpact() {
|
|
if #available(iOS 13.0, *) {
|
|
impact(style: .soft)
|
|
}
|
|
}
|
|
|
|
// Rigid impact feedback (available iOS 13+)
|
|
func rigidImpact() {
|
|
if #available(iOS 13.0, *) {
|
|
impact(style: .rigid)
|
|
}
|
|
}
|
|
|
|
// Success notification feedback
|
|
func successNotification() {
|
|
notification(type: .success)
|
|
}
|
|
|
|
// Warning notification feedback
|
|
func warningNotification() {
|
|
notification(type: .warning)
|
|
}
|
|
|
|
// Error notification feedback
|
|
func errorNotification() {
|
|
notification(type: .error)
|
|
}
|
|
|
|
// Simple selection feedback
|
|
func simpleSelection() {
|
|
selection()
|
|
}
|
|
}
|