使用背景
看上去很简单的一个需求,找了几遍RN的文档并没有说明双击事件。没有办法只能自己手动实现
代码
这个是handler的写法
const followMedia = (() => {
let times = 0;
let timer = null;
// 模仿双击事件
return () => {
clearTimeout(timer);
if (++times >= 2) {
times = 0;
// 把要执行的事件写在这
console.log('执行了双击')
}
timer = setTimeout(() => {
times = 0;
}, 500)
}
})();
直接放到onPress里执行即可
<TouchableWithoutFeedback
activeOpcity={1}
onPress={followMedia} // 绑定事件
>
<Text>双击666</Text>
</TouchableWithoutFeedback>