在React Native中发出触摸事件可以通过使用Touchable组件或者手势识别库来实现。
示例代码:
import React from 'react';
import { TouchableOpacity, Text } from 'react-native';
const MyButton = () => {
const handlePress = () => {
console.log('Button pressed');
};
return (
<TouchableOpacity onPress={handlePress}>
<Text>Press Me</Text>
</TouchableOpacity>
);
};
export default MyButton;
示例代码:
import React from 'react';
import { View, PanResponder, Text } from 'react-native';
const MyView = () => {
const panResponder = React.useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (_, gestureState) => {
console.log('Pan position:', gestureState.moveX, gestureState.moveY);
},
})
).current;
return (
<View {...panResponder.panHandlers}>
<Text>Drag Me</Text>
</View>
);
};
export default MyView;
以上是在React Native中发出触摸事件的两种常见方法。根据具体需求选择合适的方式来实现交互效果。
领取专属 10元无门槛券
手把手带您无忧上云