You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8 MAUI如何实现自定义振动反馈(如正误提示)

在.NET 8 MAUI中编排友好的振动反馈序列

当然可以编排自定义的振动序列来实现更愉悦的反馈效果,核心思路是通过异步延迟配合多次振动调用,或者利用平台原生的振动API实现更精准的模式控制。以下是具体实现方案:

基础跨平台实现(异步振动序列)

直接通过异步方法和Task.Delay控制振动间隔,无需依赖平台特定代码,适配Android和iOS:

public class VibrationService
{
    // 正确答案:短振两次+短间隔,营造轻快愉悦的反馈感
    public async Task VibrateForCorrectAnswerAsync()
    {
#if ANDROID || IOS
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(150));
        await Task.Delay(100);
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(150));
#endif
    }

    // 错误答案:长振+短振+中等间隔,传递清晰的错误提示
    public async Task VibrateForWrongAnswerAsync()
    {
#if ANDROID || IOS
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(400));
        await Task.Delay(200);
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(200));
#endif
    }
}

调用时需用await避免阻塞UI线程:

var vibrationService = new VibrationService();
await vibrationService.VibrateForCorrectAnswerAsync();

平台原生优化方案(更细腻的振动模式)

如果想要更贴合平台设计规范的振动效果,可以利用原生API通过条件编译实现:

Android 端优化

Android的Vibrator类支持直接传入振动模式数组(格式为[振动时长, 间隔时长, 振动时长, 间隔时长...]),比多次调用更高效:

#if ANDROID
using Android.OS;
#endif

public class VibrationService
{
    public void VibrateForCorrectAnswer()
    {
#if ANDROID
        var vibrator = (Vibrator)Android.App.Application.Context.GetSystemService(Android.Content.Context.VibratorService);
        if (vibrator.HasVibrator)
        {
            // 模式:等待0ms→振150ms→停100ms→振150ms
            long[] pattern = {0, 150, 100, 150};
            // -1表示只执行一次序列
            vibrator.Vibrate(pattern, -1);
        }
#elif IOS
        _ = VibrateForCorrectAnswerAsync();
#endif
    }

    public void VibrateForWrongAnswer()
    {
#if ANDROID
        var vibrator = (Vibrator)Android.App.Application.Context.GetSystemService(Android.Content.Context.VibratorService);
        if (vibrator.HasVibrator)
        {
            long[] pattern = {0, 400, 200, 200};
            vibrator.Vibrate(pattern, -1);
        }
#elif IOS
        _ = VibrateForWrongAnswerAsync();
#endif
    }

    // iOS异步实现保留
    private async Task VibrateForCorrectAnswerAsync()
    {
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(150));
        await Task.Delay(100);
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(150));
    }

    private async Task VibrateForWrongAnswerAsync()
    {
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(400));
        await Task.Delay(200);
        Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(200));
    }
}

iOS 端优化

iOS可以使用UIImpactFeedbackGenerator调用系统预设的振动反馈(轻量/中等/重型),更符合平台设计规范:

#if IOS
using UIKit;
#endif

public class VibrationService
{
    public void VibrateForCorrectAnswer()
    {
#if IOS
        var generator = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light);
        generator.Prepare();
        generator.ImpactOccurred();
        // 追加一次延迟后的轻振增强愉悦感
        _ = Task.Run(async () => {
            await Task.Delay(100);
            generator.ImpactOccurred();
        });
#elif ANDROID
        var vibrator = (Vibrator)Android.App.Application.Context.GetSystemService(Android.Content.Context.VibratorService);
        if (vibrator.HasVibrator)
        {
            long[] pattern = {0, 150, 100, 150};
            vibrator.Vibrate(pattern, -1);
        }
#endif
    }

    public void VibrateForWrongAnswer()
    {
#if IOS
        var generator = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Heavy);
        generator.Prepare();
        generator.ImpactOccurred();
        _ = Task.Run(async () => {
            await Task.Delay(200);
            generator.ImpactOccurred();
        });
#elif ANDROID
        var vibrator = (Vibrator)Android.App.Application.Context.GetSystemService(Android.Content.Context.VibratorService);
        if (vibrator.HasVibrator)
        {
            long[] pattern = {0, 400, 200, 200};
            vibrator.Vibrate(pattern, -1);
        }
#endif
    }
}

注意事项

  • Android需在AndroidManifest.xml中添加权限:<uses-permission android:name="android.permission.VIBRATE" />
  • iOS无需额外权限,但UIImpactFeedbackGenerator要求iOS 10及以上版本
  • 异步方法调用必须使用await,避免UI线程卡顿

内容的提问来源于stack exchange,提问作者Enrico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 06:36:03