-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedbackGenerator.swift
131 lines (120 loc) · 3.53 KB
/
FeedbackGenerator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// FeedbackGenerator.swift
//
// Created by Srdjan Spasojevic on 12/13/18.
// Copyright © 2018 Srdjan Spasojevic. All rights reserved.
//
import UIKit
import AudioToolbox.AudioServices
enum FeedbackType {
/**
Selection Change - Date picker changing values haptic feedback
iOS 10 required
*/
case selectionChange
/**
Light Impact - iOS 10 required
*/
case lightImpact
/**
Medium Impact - iOS 10 required
*/
case mediumImpact
/**
Heavy Impact - iOS 10 required
*/
case heavyImpact
/**
Success - iOS 10 required
*/
case success
/**
Error - iOS 10 required
*/
case error
/**
Warning - iOS 10 required
*/
case warning
/**
Peek - iOS 10 required
*/
case peek
/**
Pop
*/
case pop
/**
Cancelled -
*/
case cancelled
/**
Try Again -
*/
case tryAgain
/**
Failed - Failed login vibration feedback
*/
case failed
}
/**
Feedback Generator - Generate Haptic (Vibration) Feedback
*/
class FeedbackGenerator {
// MARK: Public methods
/**
Generate Haptic Feedback Of Given Type
Some of FeedbackType's are available only on iOS 10.0 or higher. (success, error, warning, selectionChange, light-mediun-heavy impact require iOS 10.0 or higher. peek, pop, cancelled, tryAgain, failed require iOS 9.0 or higher)
*/
static public func generateFeedback(of type: FeedbackType) {
guard iOS10Available else {
print("⚠️ iOS 10 required to use this feedback.")
return
}
let generator: UIFeedbackGenerator?
switch type {
case .success:
generator = UINotificationFeedbackGenerator()
(generator as? UINotificationFeedbackGenerator)?.notificationOccurred(.success)
case .error:
generator = UINotificationFeedbackGenerator()
(generator as? UINotificationFeedbackGenerator)?.notificationOccurred(.error)
case .warning:
generator = UINotificationFeedbackGenerator()
(generator as? UINotificationFeedbackGenerator)?.notificationOccurred(.warning)
case .selectionChange:
generator = UISelectionFeedbackGenerator()
(generator as? UISelectionFeedbackGenerator)?.selectionChanged()
case .lightImpact, .mediumImpact, .heavyImpact:
let style: UIImpactFeedbackGenerator.FeedbackStyle = {
switch type {
case .lightImpact: return .light
case .mediumImpact: return .medium
case .heavyImpact: return .heavy
default: return .light // Default to light if the type is not recognized
}
}()
generator = UIImpactFeedbackGenerator(style: style)
(generator as? UIImpactFeedbackGenerator)?.impactOccurred()
case .peek, .pop, .cancelled, .tryAgain, .failed:
playSystemSound(for: type)
generator = nil
}
generator?.prepare()
}
private static func playSystemSound(for type: FeedbackType) {
let soundID: SystemSoundID
switch type {
case .peek: soundID = 1519
case .pop: soundID = 1520
case .cancelled: soundID = 1521
case .tryAgain: soundID = 1102
case .failed: soundID = 1107
default: return
}
AudioServicesPlaySystemSound(soundID)
}
private static var iOS10Available: Bool {
return #available(iOS 10.0, *)
}
}