Xamarin iOS中的自定义振动

时间:2018-06-20 19:25:57

标签: xamarin xamarin.ios

我需要创建自定义振动(振动必须工作1秒钟),但这是我知道的唯一开始振动SystemSound.Vibrate.PlaySystemSound()的方法

我该如何实施?

2 个答案:

答案 0 :(得分:2)

不能控制触觉反馈的确切长度(就像在Android上一样),因为它会违反iOS用户界面指南。

除了较旧的Vibrate.PlaySystemSound之外,在iOS 10(+)中还添加了UIFeedbackGenerator

根据您要向用户发送的信号,有三种UIFeedbackGenerator变体:

  • UIImpactFeedbackGenerator
    • 创建触觉来模拟物理影响。
  • UISelectionFeedbackGenerator
    • 创建触觉以指示选择的变化。
  • UINotificationFeedbackGenerator
    • 创建触觉来传达成功,失败和警告。

回复:https://developer.apple.com/documentation/uikit/uifeedbackgenerator

示例:

// cache the instance
var haptic = new UINotificationFeedbackGenerator();

// Do this in advance so it is ready to be called on-demand without delay...
haptic.Prepare();

// produce the feedback as many times as needed
haptic.NotificationOccurred(UINotificationFeedbackType.Success);

// when done all done, clean up
haptic.Dispose();

答案 1 :(得分:0)

我找到了解决方案,但问题是在验证过程中应用可能被拒绝。

我使用了此链接。 Are there APIs for custom vibrations in iOS?

这是Xamarin.ios的实现

    public enum VibrationPower
    {
        Normal,
        Low,
        Hight
    }

    [DllImport("/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox")]
    private static extern void AudioServicesPlaySystemSoundWithVibration(uint inSystemSoundID, NSObject arg,
        IntPtr pattert);

    private void hightVibration()
    {
        var dictionary = new NSMutableDictionary();
        var vibePattern = new NSMutableArray();
        vibePattern.Add(NSNumber.FromBoolean(true));
        vibePattern.Add(NSNumber.FromInt32(1000));
        //vibePattern.Add(NSNumber.FromBoolean(false));
        //vibePattern.Add(NSNumber.FromInt32(500));
        //vibePattern.Add(NSNumber.FromBoolean(true));
        //vibePattern.Add(NSNumber.FromInt32(1000));
        dictionary.Add(NSObject.FromObject("VibePattern"), vibePattern);
        dictionary.Add(NSObject.FromObject("Intensity"), NSNumber.FromInt32(1));
        AudioServicesPlaySystemSoundWithVibration(4095U, null, dictionary.Handle);
    }

    public void Vibration(VibrationPower power = VibrationPower.Normal)
    {
        switch (power)
        {
                case VibrationPower.Normal:
                    SystemSound.Vibrate.PlaySystemSound();
                break;
                case VibrationPower.Hight:
                    hightVibration();
                break;
        }
    }

但是请记住,您的应用可能会在验证过程中被拒绝!!!