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

本文主要是介绍Sound/播放提示音, Haptics/触觉反馈, LocalNotification/本地通知 的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. Sound 播放提示音

  1.1 音频文件:  tada.mp3, badum.mp3

  1.2 文件位置截图:

  1.3 实现

import AVKit/// 音频管理器
class SoundManager{// 单例对象 Singletonstatic let instance = SoundManager()// 音频播放var player: AVAudioPlayer?enum SoundOption: String{case tadacase badum}func playSound(sound: SoundOption){// 获取 urlguard let url =  Bundle.main.url(forResource: sound.rawValue, withExtension: ".mp3") else { return }do{player = try AVAudioPlayer(contentsOf: url)player?.play()}catch let error{// 打印错误print("Error playing sound. \(error.localizedDescription)")}}
}/// 提示音
struct SoundsBootcamp: View {var soundManager = SoundManager()var body: some View {VStack(spacing: 40) {Button("Play sound 1") {SoundManager.instance.playSound(sound: .tada)}Button("Play sound 2") {SoundManager.instance.playSound(sound: .badum)}}}
}

2. Haptics 触觉反馈与通知

  2.1 实现

/// 触觉管理器
class HapticManager{static let instance = HapticManager()// 通知func notification(type: UINotificationFeedbackGenerator.FeedbackType){let generator = UINotificationFeedbackGenerator()generator.notificationOccurred(type)}func impact(style: UIImpactFeedbackGenerator.FeedbackStyle){// 反馈生成器let generator = UIImpactFeedbackGenerator(style: style)generator.impactOccurred()}
}/// 触觉反馈与通知
struct HapticsBootcamp: View {var body: some View {VStack(spacing: 20) {Button("Success") { HapticManager.instance.notification(type: .success) }Button("Warning") { HapticManager.instance.notification(type: .warning) }Button("Error") { HapticManager.instance.notification(type: .error) }Divider()Button("Soft") { HapticManager.instance.impact(style: .soft) }Button("Light") { HapticManager.instance.impact(style: .light) }Button("Medium") { HapticManager.instance.impact(style: .medium) }Button("Rigid") { HapticManager.instance.impact(style: .rigid) }Button("Heavy") { HapticManager.instance.impact(style: .heavy) }}}
}

3. LocalNotification 本地通知

  3.1 实现

import UserNotifications
import CoreLocation/// 通知管理类
class NotificationManager{// 单例static let instance = NotificationManager() // Singleton// 请求权限func requestAuthorization(){//let options: UNAuthorizationOptions = [.alert, .sound, .badge]UNUserNotificationCenter.current().requestAuthorization(options: options) { success, error inif let error = error {print("ERROR:\(error)")}else{print("Success:\(success)")}}}/// 加入一个通知func scheduleNotification(){let content = UNMutableNotificationContent()content.title = "This is my first notification!"content.subtitle = "This is was so easy!"content.sound = .defaultcontent.badge = 1// time 计时器通知let trigger = timeNotification()// calendar 日历通知// let trigger = calendarNotification()// location 位置通知//let trigger = locationNotificationTrigger()// 通知请求let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,// 触发器trigger: trigger)// 当前通知中心,添加一个通知UNUserNotificationCenter.current().add(request)}/// 取消通知func cancelNotification(){UNUserNotificationCenter.current().removeAllPendingNotificationRequests()UNUserNotificationCenter.current().removeAllDeliveredNotifications()}/// 位置通知func locationNotificationTrigger()-> UNNotificationTrigger{// 经纬度let coordinates = CLLocationCoordinate2D(latitude: 40.00,longitude: 50.00)// 区域 radius: 半径,以米为单位let region = CLCircularRegion(center: coordinates,radius: 100,identifier: UUID().uuidString)region.notifyOnEntry = true; // 进入region.notifyOnExit = true;  // 退出return UNLocationNotificationTrigger(region: region, repeats: true)}/// 日历通知func calendarNotification() -> UNNotificationTrigger{// calendarvar dateComponents = DateComponents()dateComponents.hour = 16dateComponents.minute = 52dateComponents.weekday = 2 // 2: 星期一// repeats 是否重复return UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)}/// 计时器通知 repeats 循环/重复func timeNotification() -> UNNotificationTrigger{return UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false);}
}/// 本地通知
struct LocalNotificationBootcamp: View {var body: some View {VStack(spacing: 40) {// 获取权限Button("Request permission") {NotificationManager.instance.requestAuthorization()}Button("Schedule notification") {NotificationManager.instance.scheduleNotification()}Button("Cancel notification") {NotificationManager.instance.cancelNotification()}}.onAppear {UIApplication.shared.applicationIconBadgeNumber = 0}}
}

  3.2 效果图:

      

这篇关于Sound/播放提示音, Haptics/触觉反馈, LocalNotification/本地通知 的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565980

相关文章

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

Java之并行流(Parallel Stream)使用详解

《Java之并行流(ParallelStream)使用详解》Java并行流(ParallelStream)通过多线程并行处理集合数据,利用Fork/Join框架加速计算,适用于大规模数据集和计算密集... 目录Java并行流(Parallel Stream)1. 核心概念与原理2. 创建并行流的方式3. 适

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo