我需要创建自定义振动(振动必须工作 1 秒(,但这是我所知道的唯一启动振动的方法SystemSound.Vibrate.PlaySystemSound()
我该如何实现它?
您无法控制触觉反馈的确切长度(就像在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();
我找到了解决方案,但问题是应用程序在验证过程中可能会被拒绝。
我使用了这个链接。 iOS 中是否有用于自定义振动的 API?
这是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;
}
}
但请记住,您的应用可能会在验证期间被拒绝!!