-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTapticEffectsService.swift
118 lines (104 loc) · 3.85 KB
/
TapticEffectsService.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
//
// TapticEffectsService.swift
//
//
// Created by Serhii Kharauzov on 10/19/17.
// Copyright © 2017 Serhii Kharauzov. All rights reserved.
//
import Foundation
import AudioToolbox.AudioServices
class TapticEffectsService {
// MARK: Public type methods - Haptic Feedback
/// Performs haptic feedback - selection.
static func performFeedbackSelection() {
if #available(iOS 10.0, *) {
guard UIDevice.current.hasHapticFeedback else { return }
let selectionFeedbackGenerator = UISelectionFeedbackGenerator()
selectionFeedbackGenerator.prepare()
selectionFeedbackGenerator.selectionChanged()
}
}
/// Performs haptic feedback - impact.
static func performFeedbackImpact(style: UIImpactFeedbackStyle) {
if #available(iOS 10.0, *) {
guard UIDevice.current.hasHapticFeedback else { return }
let mediumImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: style)
mediumImpactFeedbackGenerator.prepare()
mediumImpactFeedbackGenerator.impactOccurred()
}
}
/// Performs haptic feedback - notification.
static func performFeedbackNotification(type: UINotificationFeedbackType) {
if #available(iOS 10.0, *) {
guard UIDevice.current.hasHapticFeedback else { return }
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
notificationFeedbackGenerator.prepare()
notificationFeedbackGenerator.notificationOccurred(type)
}
}
// MARK: Public type methods - Taptic Engine
/// Performs taptic feedback based on 'TapticEngineFeedbackIdentifier'.
static func performTapticFeedback(from feedbackIdentifier: TapticEngineFeedbackIdentifier) {
if UIDevice.current.hasTapticEngine {
AudioServicesPlaySystemSound(feedbackIdentifier.rawValue)
}
}
}
extension TapticEffectsService {
enum TapticEngineFeedbackIdentifier: UInt32 {
/// 'Peek' feedback (weak boom)
case peek = 1519
/// 'Pop' feedback (strong boom)
case pop = 1520
/// 'Cancelled' feedback (three sequential weak booms)
case cancelled = 1521
/// 'Try Again' feedback (week boom then strong boom)
case tryAgain = 1102
/// 'Failed' feedback (three sequential strong booms)
case failed = 1107
}
}
// MARK: UIDevice extension
extension UIDevice {
enum DevicePlatform: String {
case other = "Old Device"
case iPhone6S = "iPhone 6S"
case iPhone6SPlus = "iPhone 6S Plus"
case iPhone7 = "iPhone 7"
case iPhone7Plus = "iPhone 7 Plus"
case iPhone8 = "iPhone 8"
case iPhone8Plus = "iPhone 8 Plus"
case iPhoneX = "iPhone X"
}
var platform: DevicePlatform {
var sysinfo = utsname()
uname(&sysinfo)
let platform = String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
switch platform {
case "iPhone10,3", "iPhone10,6":
return .iPhoneX
case "iPhone10,2", "iPhone10,5":
return .iPhone8Plus
case "iPhone10,1", "iPhone10,4":
return .iPhone8
case "iPhone9,2", "iPhone9,4":
return .iPhone7Plus
case "iPhone9,1", "iPhone9,3":
return .iPhone7
case "iPhone8,2":
return .iPhone6SPlus
case "iPhone8,1":
return .iPhone6S
default:
return .other
}
}
/// Enabled from iPhone6/iPhone6Plus.
var hasTapticEngine: Bool {
return platform != .other
}
/// Enabled from iPhone7/iPhone7Plus.
var hasHapticFeedback: Bool {
return [.iPhone7, .iPhone7Plus, .iPhone8, .iPhone8Plus, .iPhoneX].contains(platform)
}
}