沉浸式體驗:震動反饋

UINotificationFeedbackGenerator

typedef NS_ENUM(NSInteger, UINotificationFeedbackType) {
    UINotificationFeedbackTypeSuccess, 
    UINotificationFeedbackTypeWarning, 
    UINotificationFeedbackTypeError
};

- (void)showFeedback{
    
    UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
    [generator notificationOccurred:UINotificationFeedbackTypeSuccess];
}

複製代碼

UIImpactFeedbackGenerator

typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {
    UIImpactFeedbackStyleLight,
    UIImpactFeedbackStyleMedium,
    UIImpactFeedbackStyleHeavy
};

- (void)showFeedback{

    UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight];
    [generator prepare];
    [generator impactOccurred];
}

複製代碼

UISelectionFeedbackGenerator

用來模擬選擇滾輪一類控件時的震動。
- (void)showFeedback{

    UISelectionFeedbackGenerator *generator = [[UISelectionFeedbackGenerator alloc] init];
    [generator selectionChanged];
}

複製代碼

AudioToolbox ( iOS 10 以前 ):

播放系統音

List of SystemSoundIDsgit

AudioServicesPlaySystemSound (SystemSoundID);

/* --- OR --- */ 

NSURL *fileURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds/ReceivedMessage.caf"]; // See List of SystemSoundIDs
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)fileURL,&soundID);
AudioServicesPlaySystemSound(soundID);

複製代碼

播放自定義音

-(void)playSoundAfterKaoqinSuccess{
        
    NSURL *fileUrl=[[NSBundle mainBundle] URLForResource:@"kaoqin_success.caf" withExtension:nil];
    
    SystemSoundID soundID=0;
    
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallBack, NULL);
    AudioServicesPlaySystemSound(soundID);
}

void soundCompleteCallBack(SystemSoundID soundID, void * clientDate) {
    
    AudioServicesRemoveSystemSoundCompletion(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
}

複製代碼
相關文章
相關標籤/搜索