【问题标题】:TouchableWithoutFeedback never fires touch eventsTouchableWithoutFeedback 从不触发触摸事件
【发布时间】:2019-01-13 02:24:24
【问题描述】:

我有以下 React-Native 代码,它会提醒用户它被按下,但它没有响应任何内容。

我尝试使用不同的Touchable 组件,但似乎没有一个可以工作,唯一的一个是Button,但这不是我真正想要的,我正在构建一个滑块,所以按钮并不适合这个。

无论如何,这是我的代码:

import React, {Component} from 'react';
import {View, TouchableWithoutFeedback, Alert, Text} from 'react-native';
import styles from './styles.js';
import LinearGradient from 'react-native-linear-gradient';

export default class BrightnessSlider extends Component {
    value: 100;

    constructor(...args) {
        super(...args);
    }

    render() {
        return (
            <LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={["#222", "#ddd"]} style={styles.brightnessSliderRunnable}>
                <View style={styles.brightnessSliderTrack}>
                    <TouchableWithoutFeedback onPress={() => Alert.alert("Hello World")}>
                        <View style={styles.brightnessSliderThumb} />
                    </TouchableWithoutFeedback>
                </View>
            </LinearGradient>
        )
    }
}

./styles.js

import {StyleSheet} from "react-native";

export default styles = StyleSheet.create({
    brightnessSliderRunnable: {
        top: 50,
        height: 9,
        width: width - 20,
        left: 10,
        backgroundColor: "#555",
        opacity: 1,
        elevation: 1,
        borderWidth: 0
        // position: "absolute"

    },
    brightnessSliderThumb: {
        width: 23,
        height: 23,
        borderColor: "#888",
        // opacity: 1,
        borderWidth: 2,
        backgroundColor: "#ffffff",
        position: "absolute",
        top: -7,
        borderRadius: 5,
        elevation: 2
    },
    brightnessSliderTrack: {
        width: "100%",
        elevation: 1,
        borderWidth: 0
    }
});

感谢您的帮助

【问题讨论】:

    标签: react-native touchablewithoutfeedback


    【解决方案1】:

    提问者原code

    解决方案

    由提问者的解决方案添加。


    将位置:absolute 更改为位置:relative


    如下更改由 StyleSheet 制作的样式类型。

    您的原件。

    {...styles.colourContainer, opacity: 100 - this.darkness}
    

    推荐方式。

    [styles.colourContainer, {opacity: 100 - this.darkness}]
    

    我测试了代码,当我们触摸白色方块时它运行良好。

    在 iOS 上测试。

    App.js

    App.js 由我更改。

    import React, {Component} from 'react';
    import {View, Dimensions, Text} from 'react-native';
    
    import BrightnessSlider from '../components/BrightnessSlider';
    import styles from '../components/TempStyle.js';
    
    const d = Dimensions.get('window'),
      width = d.width,
      height = d.height;
    
    export default class BrightnessSliderTest extends Component {
      constructor(...args) {
        super(...args);
    
        this.darkness = 0;
    
        this.prevColours = ["#ff0000", "#00ff00", "#0000ff"];
      }
    
      render() {
    
        // It is not possible just access the style created by StyleSheet.create(). Do not use StyleSheet.create() or change to flatten the StyleSheet object.
        const sty = [styles.colourContainer,{ opacity: 100 - this.darkness}];
    
        return (
          <View style={styles.container}>
            <View style={sty}>
              <View style={styles.toolBar}>
                <View style={styles.colourPalette}>
                  {this.prevColours.map((i, a) => <View key={String(a)} style={[styles.colourTile, {backgroundColor: i}]}/>)}
                </View>
                {/* <Button title={"Save To Palette"} onTap={this.saveToPalette}>Save</Button> */}
              </View>
              <BrightnessSlider />
            </View>
          </View>
        );
      }
    }

    为什么?

    我不确定如何在其他组件中使用您的组件。但是如果你没有足够大小的容器来展示你的组件,触摸区域可能无法工作。这就是为什么我有时在测试中使用 backgroundColor 作为容器。


    在测试所使用的完整代码后添加。

    不可能只通过 StyleSheet.create() 创建的 like spread 运算符来访问样式。不要使用 StyleSheet.create() 或更改来展平 StyleSheet 对象。

    样式的工作方式与平台(iOS、Android)不同。

    【讨论】:

    • 我在 3 个不同的 react-native 项目中尝试过这个,结果相同,“再试一次”没有帮助
    • 会不会和平台有关?
    • @JacobSchneider 或尝试将LinearGradient 转换为普通View
    • 我更新了答案。尝试使用我的代码风格。只有当你触摸你知道的白色方块时它才会起作用。我不确定您如何在其他人中使用您的组件。但是如果你没有足够大小的容器来展示你的组件,触摸区域可能无法工作。这就是为什么我有时在测试中使用backgroundColor 作为容器。希望你能找到解决办法。
    • 嘿,非常感谢您的回答,但不幸的是,没有任何效果,我已经被困在这个问题上超过 DAYS
    猜你喜欢
    • 1970-01-01
    • 2015-11-06
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多