• Sound/播放提示音, Haptics/触觉反馈, LocalNotification/本地通知 的使用


    1. Sound 播放提示音

      1.1 音频文件:  tada.mp3badum.mp3

      1.2 文件位置截图:

      1.3 实现

    1. import AVKit
    2. /// 音频管理器
    3. class SoundManager{
    4. // 单例对象 Singleton
    5. static let instance = SoundManager()
    6. // 音频播放
    7. var player: AVAudioPlayer?
    8. enum SoundOption: String{
    9. case tada
    10. case badum
    11. }
    12. func playSound(sound: SoundOption){
    13. // 获取 url
    14. guard let url = Bundle.main.url(forResource: sound.rawValue, withExtension: ".mp3") else { return }
    15. do{
    16. player = try AVAudioPlayer(contentsOf: url)
    17. player?.play()
    18. }catch let error{
    19. // 打印错误
    20. print("Error playing sound. \(error.localizedDescription)")
    21. }
    22. }
    23. }
    24. /// 提示音
    25. struct SoundsBootcamp: View {
    26. var soundManager = SoundManager()
    27. var body: some View {
    28. VStack(spacing: 40) {
    29. Button("Play sound 1") {
    30. SoundManager.instance.playSound(sound: .tada)
    31. }
    32. Button("Play sound 2") {
    33. SoundManager.instance.playSound(sound: .badum)
    34. }
    35. }
    36. }
    37. }

    2. Haptics 触觉反馈与通知

      2.1 实现

    1. /// 触觉管理器
    2. class HapticManager{
    3. static let instance = HapticManager()
    4. // 通知
    5. func notification(type: UINotificationFeedbackGenerator.FeedbackType){
    6. let generator = UINotificationFeedbackGenerator()
    7. generator.notificationOccurred(type)
    8. }
    9. func impact(style: UIImpactFeedbackGenerator.FeedbackStyle){
    10. // 反馈生成器
    11. let generator = UIImpactFeedbackGenerator(style: style)
    12. generator.impactOccurred()
    13. }
    14. }
    15. /// 触觉反馈与通知
    16. struct HapticsBootcamp: View {
    17. var body: some View {
    18. VStack(spacing: 20) {
    19. Button("Success") { HapticManager.instance.notification(type: .success) }
    20. Button("Warning") { HapticManager.instance.notification(type: .warning) }
    21. Button("Error") { HapticManager.instance.notification(type: .error) }
    22. Divider()
    23. Button("Soft") { HapticManager.instance.impact(style: .soft) }
    24. Button("Light") { HapticManager.instance.impact(style: .light) }
    25. Button("Medium") { HapticManager.instance.impact(style: .medium) }
    26. Button("Rigid") { HapticManager.instance.impact(style: .rigid) }
    27. Button("Heavy") { HapticManager.instance.impact(style: .heavy) }
    28. }
    29. }
    30. }

    3. LocalNotification 本地通知

      3.1 实现

    1. import UserNotifications
    2. import CoreLocation
    3. /// 通知管理类
    4. class NotificationManager{
    5. // 单例
    6. static let instance = NotificationManager() // Singleton
    7. // 请求权限
    8. func requestAuthorization(){
    9. //
    10. let options: UNAuthorizationOptions = [.alert, .sound, .badge]
    11. UNUserNotificationCenter.current().requestAuthorization(options: options) { success, error in
    12. if let error = error {
    13. print("ERROR:\(error)")
    14. }else{
    15. print("Success:\(success)")
    16. }
    17. }
    18. }
    19. /// 加入一个通知
    20. func scheduleNotification(){
    21. let content = UNMutableNotificationContent()
    22. content.title = "This is my first notification!"
    23. content.subtitle = "This is was so easy!"
    24. content.sound = .default
    25. content.badge = 1
    26. // time 计时器通知
    27. let trigger = timeNotification()
    28. // calendar 日历通知
    29. // let trigger = calendarNotification()
    30. // location 位置通知
    31. //let trigger = locationNotificationTrigger()
    32. // 通知请求
    33. let request = UNNotificationRequest(
    34. identifier: UUID().uuidString,
    35. content: content,
    36. // 触发器
    37. trigger: trigger)
    38. // 当前通知中心,添加一个通知
    39. UNUserNotificationCenter.current().add(request)
    40. }
    41. /// 取消通知
    42. func cancelNotification(){
    43. UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    44. UNUserNotificationCenter.current().removeAllDeliveredNotifications()
    45. }
    46. /// 位置通知
    47. func locationNotificationTrigger()-> UNNotificationTrigger{
    48. // 经纬度
    49. let coordinates = CLLocationCoordinate2D(
    50. latitude: 40.00,
    51. longitude: 50.00)
    52. // 区域 radius: 半径,以米为单位
    53. let region = CLCircularRegion(
    54. center: coordinates,
    55. radius: 100,
    56. identifier: UUID().uuidString)
    57. region.notifyOnEntry = true; // 进入
    58. region.notifyOnExit = true; // 退出
    59. return UNLocationNotificationTrigger(region: region, repeats: true)
    60. }
    61. /// 日历通知
    62. func calendarNotification() -> UNNotificationTrigger{
    63. // calendar
    64. var dateComponents = DateComponents()
    65. dateComponents.hour = 16
    66. dateComponents.minute = 52
    67. dateComponents.weekday = 2 // 2: 星期一
    68. // repeats 是否重复
    69. return UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    70. }
    71. /// 计时器通知 repeats 循环/重复
    72. func timeNotification() -> UNNotificationTrigger{
    73. return UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false);
    74. }
    75. }
    76. /// 本地通知
    77. struct LocalNotificationBootcamp: View {
    78. var body: some View {
    79. VStack(spacing: 40) {
    80. // 获取权限
    81. Button("Request permission") {
    82. NotificationManager.instance.requestAuthorization()
    83. }
    84. Button("Schedule notification") {
    85. NotificationManager.instance.scheduleNotification()
    86. }
    87. Button("Cancel notification") {
    88. NotificationManager.instance.cancelNotification()
    89. }
    90. }
    91. .onAppear {
    92. UIApplication.shared.applicationIconBadgeNumber = 0
    93. }
    94. }
    95. }

      3.2 效果图:

          

  • 相关阅读:
    2024年最新通信安全员考试题库
    Mysql.索引篇 一次搞清楚八股文
    Vitalik详解5种类型的ZK-EVM
    Oracle列转行SQL语句_01
    【JavaSpring】Aop切入点表达式
    DevTools failed to parse SourceMap 警告解决方法
    时间序列分析中的自相关
    产品经理应具备的能力
    华为终于要“三分天下”
    Java.lang.Class类 getResource()方法有什么功能呢?
  • 原文地址:https://blog.csdn.net/u011193452/article/details/133316956