uniapp 配置长按事件

在uni-app中长按事件使用的是 @longpress 或者 @longtap

但在使用中会出现滑动时同样会触发长按事件

因此我们需要做到在页面滑动时不触发长按事件

1、绑定事件,利用长按事件、触摸结束、触摸中完成

1
<view @longpress="longPress" @touchend="touchEnd" @touchmove="touchMove"></view>

2、声明初始化是否为长按ifLongtap

1
2
3
4
5
data(){
return {
ifLongtap: true
}
}

3、利用触摸中和触摸结束来完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 长按
longPress(i) {
if (this.ifLongtap) {
// 长按后触发的事件
console.log('长按');
}
},
// 移动
touchEnd() {
this.ifLongtap = true;
},
touchMove() {
// 手指触摸后的移动事件
this.ifLongtap = false;
},