1. 触感反馈的使用(UIFeedbackGenerator)
提示:
UIFeedbackGenerator在 iOS 10.0 及更新版本可用。
1.1 触感反馈工具类 FeedbackGeneratorUtil 的使用流程
- 导入
FeedbackGeneratorUtil类; - 确定需要触感反馈的操作;
- 在操作事件中调用
FeedbackGeneratorUtil的方法即可产生触感反馈。
1.2 触感反馈工具类 FeedbackGeneratorUtil 的使用方法
// 产生触感反馈,该方法默认为中度反馈 [FeedbackGeneratorUtil generateImpactFeedback]; // 根据传入的触感反馈类型产生触感反馈 [FeedbackGeneratorUtil generateImpactFeedbackWithStyle:UIImpactFeedbackStyleMedium];
2. 触感反馈使用建议
| 触感反馈强度 | 建议的操作 | 示例使用场景 |
|---|---|---|
| UIImpactFeedbackStyleLight 轻度反馈 |
1. 选中操作 | ↴ |
| UITableView列表选中某一行时 | ||
| 登录、注册等页面,点击密码显示/隐藏按钮时 | ||
| 2. 成功操作 | ↴ | |
| 设备打开/关闭时 | ||
| 3. 失败操作 | ↴ | |
| 接口获取数据失败时 | ||
| UIImpactFeedbackStyleMedium 中度反馈 |
1. 一般弹窗提示操作 | ↴ |
| 点击退出登录按钮,弹出提示弹窗时 | ||
| 两次密码输入不一致,弹出提示弹窗时 | ||
| 2. 成功操作 | ↴ | |
| 扫码识别成功时 | ||
| UIImpactFeedbackStyleHeavy 重度反馈 |
1. 删除操作 | ↴ |
| 点击删除设备按钮,弹出提示弹窗时 | ||
| 点击注销账户按钮,弹出提示弹窗时 |
提示:示例使用场景,并不是每个场景都需要使用,可根据App使用体验自行决定何时、何地使用。
3. 触感反馈工具类 FeedbackGeneratorUtil 源代码
3.1 FeedbackGeneratorUtil.h
// // FeedbackGeneratorUtil.h // // Created by wangzhi on 2022/7/15. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> NS_ASSUME_NONNULL_BEGIN @interface FeedbackGeneratorUtil : NSObject // MARK: 添加触感反馈 /// 产生触感反馈效果 /// /// 1. iOS 13.0之前, 无强度参数 intensity /// /// 2. iOS 13.0之后, 增加强度参数 intensity , 即可以指定触感反馈强度 /// /// - intensity 设置为0.0时, 无触感反馈 /// /// - intensity 设置为1.0时, 其强度等价于 iOS 13.0之前的无intensity时的强度 /// /// @param style 触感反馈类型 /// @param intensity 触感反馈强度 [0.0, 1.0] + (void)generateImpactFeedbackWithStyle:(UIImpactFeedbackStyle)style intensity:(CGFloat)intensity; /// 产生触感反馈效果 /// @param style 触感反馈类型 + (void)generateImpactFeedbackWithStyle:(UIImpactFeedbackStyle)style; /// 产生触感反馈效果 + (void)generateImpactFeedback; // MARK: 播放声音 /// 使用系统声音服务播放系统声音 /// /// Apple官方公布的系统铃声列表: http://iphonedevwiki.net/index.php/AudioServices /// /// @param soundID 声音ID, UInt32类型 + (void)playSystemSoundWithSoundID:(SystemSoundID)soundID; /// 使用系统声音服务播放指定的声音文件 /// @param name 声音文件名称 /// @param type 声音文件类型 + (void)playSoundWithName:(NSString *)name type:(NSString *)type; @end NS_ASSUME_NONNULL_END
3.2 FeedbackGeneratorUtil.m
// // FeedbackGeneratorUtil.m // // Created by wangzhi on 2022/7/15. // #import "FeedbackGeneratorUtil.h" @implementation FeedbackGeneratorUtil // MARK: 触感反馈 /// 产生触感反馈效果 /// /// 1. iOS 13.0之前, 无强度参数 intensity /// /// 2. iOS 13.0之后, 增加强度参数 intensity , 即可以指定触感反馈强度 /// /// - intensity 设置为0.0时, 无触感反馈 /// /// - intensity 设置为1.0时, 其强度等价于 iOS 13.0之前的无intensity时的强度 /// /// @param style 触感反馈类型 /// @param intensity 触感反馈强度 [0.0, 1.0] + (void)generateImpactFeedbackWithStyle:(UIImpactFeedbackStyle)style intensity:(CGFloat)intensity { if (@available(iOS 10.0, *)) { UIImpactFeedbackGenerator *feedbackGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:style]; [feedbackGenerator prepare]; if (@available(iOS 13.0, *)) { [feedbackGenerator impactOccurredWithIntensity:intensity]; } else { [feedbackGenerator impactOccurred]; } } } /// 产生触感反馈效果 /// @param style 触感反馈类型 + (void)generateImpactFeedbackWithStyle:(UIImpactFeedbackStyle)style { [self generateImpactFeedbackWithStyle:style intensity:1.0]; } /// 产生触感反馈效果 + (void)generateImpactFeedback { [self generateImpactFeedbackWithStyle:UIImpactFeedbackStyleMedium]; } // MARK: 播放声音 /// 使用系统声音服务播放系统声音 /// /// Apple官方公布的系统铃声列表: http://iphonedevwiki.net/index.php/AudioServices /// /// .mp3 转 .caf 的终端命令: afconvert mp3文件路径 目标caf文件路径 -d ima4 -f caff -v /// 例如: afconvert /Users/xxx/Desktop/demo.mp3 /Users/xxx/Desktop/demo.caf -d ima4 -f caff -v /// /// 由于自定义通知声音还是由 iOS 系统来播放的,所以对音频数据格式有限制,可以是如下四种之一: /// Linear PCM MA4 (IMA/ADPCM) µLaw aLaw /// 对应音频文件格式是 aiff,wav,caf 文件,文件也必须放到 app 的 mainBundle 目录中。 /// /// 自定义通知声音的播放时间必须在 30s 内,如果超过这个限制,则将用系统默认通知声音替代。 /// /// @param soundID 声音ID, UInt32类型 + (void)playSystemSoundWithSoundID:(SystemSoundID)soundID { // The system sound ID in the range 1000 to 2000 if (soundID < 1000 || soundID > 2000) { NSLog(@"The system soundID in the range 1000 to 2000"); soundID = 1000; } // 通过音效ID播放声音 if (@available(iOS 9.0, *)) { AudioServicesPlaySystemSoundWithCompletion(soundID, ^{ }); // 震动 // AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, ^{ // }); // 通过音效ID播放声音并带有震动 // AudioServicesPlayAlertSoundWithCompletion(soundID, ^{ // }); } else { AudioServicesPlaySystemSound(soundID); } } /// 使用系统声音服务播放指定的声音文件 /// @param name 声音文件名称 /// @param type 声音文件类型 + (void)playSoundWithName:(NSString *)name type:(NSString *)type { if (name.length == 0) { return; } // 1. 获取声音文件的路径 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:type]; if (soundFilePath.length == 0) { return; } // 将地址字符串转换成url NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath isDirectory:NO]; // 2. 生成系统音效ID SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundFileURL, &soundID); // 3. 通过音效ID播放声音 if (@available(iOS 9.0, *)) { AudioServicesPlaySystemSoundWithCompletion(soundID, ^{ }); // 震动 // AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, ^{ // }); // 通过音效ID播放声音并带有震动 // AudioServicesPlayAlertSoundWithCompletion(soundID, ^{ // }); } else { AudioServicesPlaySystemSound(soundID); } } @end