Skip to content

Instantly share code, notes, and snippets.

View saoudrizwan's full-sized avatar

Saoud Rizwan saoudrizwan

View GitHub Profile
// Initialize in viewDidLoad
let mediumImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
// Prepare in viewDidAppear
mediumImpactFeedbackGenerator.prepare()
// Play haptic signal
if let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int {
switch feedbackSupportLevel {
case 2:
// Initialize in viewDidLoad
var mediumImpactFeedbackGenerator: UIImpactFeedbackGenerator? = nil
if UIDevice.current.hasHapticFeedback {
mediumImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
}
// Prepare in viewDidAppear
mediumImpactFeedbackGenerator?.prepare()
// Play the desired haptic signal
import UIKit
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"
}
import UIKit
let selectionFeedbackGenerator = UISelectionFeedbackGenerator()
// Prepare shortly before playing
selectionFeedbackGenerator.prepare()
// Play the haptic signal
selectionFeedbackGenerator.selectionChanged()
import UIKit
let impactFeedbackGenerator: (
light: UIImpactFeedbackGenerator,
medium: UIImpactFeedbackGenerator,
heavy: UIImpactFeedbackGenerator) = (
UIImpactFeedbackGenerator(style: .light),
UIImpactFeedbackGenerator(style: .medium),
UIImpactFeedbackGenerator(style: .heavy)
)
import UIKit
let lightImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
// Prepare shortly before playing
lightImpactFeedbackGenerator.prepare()
// Play the haptic signal
lightImpactFeedbackGenerator.impactOccurred()
@saoudrizwan
saoudrizwan / HapticFeedback-Notification.swift
Last active July 17, 2018 18:44
Haptic feedback UINotificationFeedbackGenerator example for iOS 10
import UIKit
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
// We can inform the feedback generator that it will likely receive events soon,
// which basically 'wakes up' the device's taptic engine for a few seconds,
// preparing it for use without any latency. You can call this function as many
// times as you want. Good times to call this are when a gesture begins and right
// after a haptic signal is played.
notificationFeedbackGenerator.prepare()
@saoudrizwan
saoudrizwan / TapticEngineHapticSignals.swift
Last active May 21, 2023 22:21
Example of using Taptic Engine haptic signals on iOS
import AudioToolbox.AudioServices
// 'Peek' feedback (weak boom)
let peek = SystemSoundID(1519)
AudioServicesPlaySystemSound(peek)
// 'Pop' feedback (strong boom)
let pop = SystemSoundID(1520)
AudioServicesPlaySystemSound(pop)
@saoudrizwan
saoudrizwan / BasicVibration.swift
Last active August 15, 2017 04:58
Basic vibration example for iOS
import AudioToolbox.AudioServices
// kSystemSoundID_Vibrate is just a UInt32 with a value of 4095
let vibrate = SystemSoundID(kSystemSoundID_Vibrate)
AudioServicesPlaySystemSound(vibrate)
// ... or if you want a completion block to be called after the vibration finishes
let vibrate = SystemSoundID(kSystemSoundID_Vibrate) // 4095
AudioServicesPlaySystemSoundWithCompletion(vibrate, {
print("did vibrate")
import Disk
do {
try Disk.save(posts, to: .documents, as: "posts.json")
let retrieved = try Disk.retrieve("posts.json", from: .documents, as: [Post].self)
} catch {
// ...
}