Files
jindengchen-ai-report/cool-unix/uni_modules/cool-vibrate/utssdk/app-ios/index.uts
2025-11-13 10:36:23 +08:00

43 lines
1.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as AudioToolbox from "AudioToolbox";
import * as UIKit from "UIKit";
import { Thread } from "Foundation";
/**
* 触发设备震动
* @param duration 震动持续时间,单位:毫秒(ms)仅在iOS 13.0+有效
*/
export function vibrate(duration: number) {
// 参数验证确保duration为正数
if (duration < 0) {
duration = 0;
}
// 检查iOS版本决定使用哪种震动方式
if (UTSiOS.available("iOS 13.0, *")) {
// 创建中等强度的触觉反馈生成器
const generator = new UIKit.UIImpactFeedbackGenerator(
(style = UIKit.UIImpactFeedbackGenerator.FeedbackStyle.medium)
);
// 准备生成器,提高首次触发的响应速度
generator.prepare();
// 记录开始时间
const startTime = new Date().getTime();
const endTime = startTime + duration;
// 循环产生震动效果,直到达到指定时长
while (new Date().getTime() < endTime) {
// 触发触觉反馈强度为0.5(中等强度)
generator.impactOccurred((intensity = 0.5));
// 暂停100毫秒避免过于频繁的震动
Thread.sleep((forTimeInterval = 0.1));
}
} else {
// iOS 13.0以下版本使用AudioToolbox播放系统震动音效
// 注意此方式无法控制震动时长duration参数将被忽略
AudioToolbox.AudioServicesPlayAlertSound(AudioToolbox.kSystemSoundID_Vibrate);
}
}