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