在React Native中,可以通过设置TouchableOpacity组件的pointerEvents属性来控制是否穿透。默认情况下,TouchableOpacitypointerEvents属性为auto,表示可以穿透。如果要禁止穿透,可以将pointerEvents属性设置为none

以下是一个示例:

import React from 'react';
import { View, TouchableOpacity, Text } from 'react-native';

const App = () => {
  const handlePress = () => {
    console.log('TouchableOpacity被点击');
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <TouchableOpacity
        style={{ backgroundColor: 'red', padding: 10 }}
        onPress={handlePress}
        pointerEvents="none" // 设置pointerEvents为none,禁止穿透
      >
        <Text style={{ color: 'white' }}>TouchableOpacity</Text>
      </TouchableOpacity>
    </View>
  );
};

export default App;

在上述示例中,TouchableOpacity被设置为禁止穿透,所以当点击TouchableOpacity时,不会触发handlePress函数。


原文地址: https://www.cveoy.top/t/topic/icEl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录