[英]How can I fire an event using onLongPress in React native?
I'm using react-native!我正在使用本机反应! I want to use TouchableOpacity so that when a button is pressed for about 0.5 seconds, the hellofunc function is executed and an alert('Button Long Pressed');
我想使用 TouchableOpacity 以便当按下按钮约 0.5 秒时,将执行 hellofunc 函数并发出警报('Button Long Pressed'); is displayed.
被展示。
However, using my code, I have to click and hold the button twice instead of once to run it.但是,使用我的代码,我必须单击并按住按钮两次而不是一次才能运行它。
How can I fix my code?如何修复我的代码?
this is my code这是我的代码
const App = () => {
const hellofunc = () => {
alert('Button Long Pressed');
};
return (
<TouchableOpacity style={styles.main} onLongPress={hellofunc}>
<Text>hi</Text>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
main: {
backgroundColor: 'lightblue',
width: '100%',
height: 30,
},
});
export default App;
There is no direct way, but you can add two events keyUp and keyDown.没有直接的方法,但是可以添加两个事件 keyUp 和 keyDown。 Make a variable for time when the time taken is more than 0.5 you can set status as
long pressed key
.为时间设置一个变量,当所用时间大于 0.5 时,您可以将状态设置为
long pressed key
。
let timeElapsed = 0;
let status = null;
document.addEventListener('keydown', checkLongPress);
document.addEventListener('keyUp', checkLongPress);
const getTimeMS = new Date().getTime(); // in ms
function checkLongPress() {
timeElapsed = timeElapsed === 0 getTimeMS() ? : getTimeMS() - timeElapsed;
if (timeElapsed /1000 >= threshold) {
status = 'Long Pressed';
timeElapsed = 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.