iOS開發(fā) 觸覺體驗(UIFeedbackGenerator)的使用

1. 觸感反饋的使用(UIFeedbackGenerator)

提示:UIFeedbackGenerator在 iOS 10.0 及更新版本可用。

1.1 觸感反饋工具類 FeedbackGeneratorUtil 的使用流程
  1. 導入FeedbackGeneratorUtil類;
  2. 確定需要觸感反饋的操作;
  3. 在操作事件中調用FeedbackGeneratorUtil的方法即可產生觸感反饋。
1.2 觸感反饋工具類 FeedbackGeneratorUtil 的使用方法
// 產生觸感反饋,該方法默認為中度反饋
[FeedbackGeneratorUtil generateImpactFeedback];

// 根據(jù)傳入的觸感反饋類型產生觸感反饋
[FeedbackGeneratorUtil generateImpactFeedbackWithStyle:UIImpactFeedbackStyleMedium];

2. 觸感反饋使用建議

觸感反饋強度 建議的操作 示例使用場景
UIImpactFeedbackStyleLight
輕度反饋
1. 選中操作 ?
UITableView列表選中某一行時
登錄、注冊等頁面,點擊密碼顯示/隱藏按鈕時
2. 成功操作 ?
設備打開/關閉時
3. 失敗操作 ?
接口獲取數(shù)據(jù)失敗時
UIImpactFeedbackStyleMedium
中度反饋
1. 一般彈窗提示操作 ?
點擊退出登錄按鈕,彈出提示彈窗時
兩次密碼輸入不一致,彈出提示彈窗時
2. 成功操作 ?
掃碼識別成功時
UIImpactFeedbackStyleHeavy
重度反饋
1. 刪除操作 ?
點擊刪除設備按鈕,彈出提示彈窗時
點擊注銷賬戶按鈕,彈出提示彈窗時

提示:示例使用場景,并不是每個場景都需要使用,可根據(jù)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之前, 無強度參數(shù) intensity
///
/// 2. iOS 13.0之后, 增加強度參數(shù) 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: 播放聲音

/// 使用系統(tǒng)聲音服務播放系統(tǒng)聲音
///
/// Apple官方公布的系統(tǒng)鈴聲列表: http://iphonedevwiki.net/index.php/AudioServices
///
/// @param soundID 聲音ID, UInt32類型
+ (void)playSystemSoundWithSoundID:(SystemSoundID)soundID;

/// 使用系統(tǒng)聲音服務播放指定的聲音文件
/// @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之前, 無強度參數(shù) intensity
///
/// 2. iOS 13.0之后, 增加強度參數(shù) 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: 播放聲音

/// 使用系統(tǒng)聲音服務播放系統(tǒng)聲音
///
/// Apple官方公布的系統(tǒng)鈴聲列表: 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 系統(tǒng)來播放的,所以對音頻數(shù)據(jù)格式有限制,可以是如下四種之一:
/// Linear PCM       MA4 (IMA/ADPCM)       μLaw        aLaw
/// 對應音頻文件格式是 aiff,wav,caf 文件,文件也必須放到 app 的 mainBundle 目錄中。
///
/// 自定義通知聲音的播放時間必須在 30s 內,如果超過這個限制,則將用系統(tǒng)默認通知聲音替代。
///
/// @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);
    }
}

/// 使用系統(tǒng)聲音服務播放指定的聲音文件
/// @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. 生成系統(tǒng)音效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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容