6

uniapp如何开启短震动_触感反馈实现

 1 year ago
source link: https://www.fly63.com/article/detial/12239
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用 uni-app 开发一款 app,需求中有一项是点击 触感反馈,查阅了 uni-app 相关文档,发现并没有对应的 api,最开始尝试用“震动”的方式来模拟“触感反馈” , 但是感觉效果并不好,因为在ios中并不支持短震动(15ms), 只支持长震动(400ms)。在这里记录一下最后的处理方式。

uniapp官网文档

在文档中只找到了uni.vibrateShort的方法,来开启短震动。对应安卓效果还行,代码如下:

uni.vibrateShort({
success: ()=>{
console.log('success');
}
});

注意

  • iOS上只有长震动,没有短震动
  • iOS上需要手机设置“打开响铃时震动”或“静音时震动”,否则无法震动

IOS 开启触感反馈

iPhone 使用触感反馈需要手机支持并且在设置中打开,否则并不能触发。所以必须使用H5+的api方法,其核心代码如下:

let UIImpactFeedbackGenerator = plus.ios.importClass(
'UIImpactFeedbackGenerator'
)
let impact = new UIImpactFeedbackGenerator()
impact.prepare()
impact.init(1)
impact.impactOccurred()

在需要在需要触感反馈的地方,把这段代码复制进去即可!

针对安卓和ios不同的实现,最终实现公用代码如下:

onFeedTap() {
let platform=uni.getSystemInfoSync().platform
// #ifdef APP-PLUS
if (platform == "ios") {
let UIImpactFeedbackGenerator = plus.ios.importClass('UIImpactFeedbackGenerator');
let impact = new UIImpactFeedbackGenerator();
impact.prepare();
impact.init(1);
impact.impactOccurred();
}
if (platform == "android") {
uni.vibrateShort();
}
// #endif
},

链接: https://www.fly63.com/article/detial/12239


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK