BannerHapticGenerator.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2017-2018 Dalton Hinterscher
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  9. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  10. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  11. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12. */
  13. import UIKit
  14. public enum BannerHaptic {
  15. case light
  16. case medium
  17. case heavy
  18. case none
  19. @available(iOS 10.0, *)
  20. var impactStyle: UIImpactFeedbackGenerator.FeedbackStyle? {
  21. switch self {
  22. case .light: return .light
  23. case .medium: return .medium
  24. case .heavy: return .heavy
  25. case .none: return nil
  26. }
  27. }
  28. }
  29. open class BannerHapticGenerator: NSObject {
  30. /**
  31. Generates a haptic based on the given haptic
  32. -parameter haptic: The haptic strength to generate when a banner is shown
  33. */
  34. open class func generate(_ haptic: BannerHaptic) {
  35. if #available(iOS 10.0, *) {
  36. if let style = haptic.impactStyle {
  37. let feedbackGenerator = UIImpactFeedbackGenerator(style: style)
  38. feedbackGenerator.prepare()
  39. feedbackGenerator.impactOccurred()
  40. }
  41. }
  42. }
  43. }