Last active
September 12, 2020 14:35
-
-
Save SwapnanilDhol/2c8e354c2e7dc02e76d7f801df82ac08 to your computer and use it in GitHub Desktop.
A Swift Helper class to generate different types of haptics from one class. Just initialize this once in your view controller and call the methods when you need them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************************************************** | |
* FeedbackGenerators.swift | |
* Neon: Capture, Edit, Share | |
***************************************************************************** | |
* Copyright (c) 2020 Neon. All rights reserved. | |
* | |
* Authors: Swapnanil Dhol <swapnanildhol # gmail.com> | |
* | |
* Refer to the COPYING file of the official project for license. | |
*****************************************************************************/ | |
import UIKit | |
enum HapticsIntensity: CGFloat { | |
case buttonTapLight = 0.3 | |
case buttonTapMedium = 0.4 | |
case buttonTapHard = 0.5 | |
case cellTap = 0.2 | |
case barButtonTap = 0.25 | |
case segmentValueChange = 0.45 | |
} | |
class HapticsHelper { | |
private let impactFeedbackGenerator = UIImpactFeedbackGenerator() | |
private let notificationFeedbackGenerator = UINotificationFeedbackGenerator() | |
init() { | |
impactFeedbackGenerator.prepare() | |
notificationFeedbackGenerator.prepare() | |
} | |
func lightButtonTap() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.buttonTapLight.rawValue) | |
} | |
func mediumButtonTap() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.buttonTapMedium.rawValue) | |
} | |
func hardButtonTap() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.buttonTapHard.rawValue) | |
} | |
func cellTap() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.cellTap.rawValue) | |
} | |
func barButtonTap() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.barButtonTap.rawValue) | |
} | |
func segmentChangedValue() { | |
impactFeedbackGenerator.impactOccurred(intensity: HapticsIntensity.segmentValueChange.rawValue) | |
} | |
func success() { | |
notificationFeedbackGenerator.notificationOccurred(.success) | |
} | |
func warning() { | |
notificationFeedbackGenerator.notificationOccurred(.warning) | |
} | |
func error() { | |
notificationFeedbackGenerator.notificationOccurred(.error) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private let hapticsHelper = HapticsHelper() | |
private func barButtonTapped(_ sender: UIBarButtonItem) { | |
hapticsHelper.barButtonTapped() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment