TouchableWithoutFeedback永远不会触发触摸事件

时间:2019-01-13 02:24:24

标签: react-native touchablewithoutfeedback

我有以下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
    }
});

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

提问者的原始code

解决方案

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


将位置absolute更改为位置relative


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

您的原始照片。

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

推荐方式。

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

我测试了代码,当我们触摸白色方块时效果很好。

在iOS上测试。

enter image description here

原始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()创建的传播运算符来访问样式。不要使用StyleSheet.create()或进行更改以展平StyleSheet对象。

样式可以与平台(iOS,Android)不同地工作。

相关问题