代码之家  ›  专栏  ›  技术社区  ›  J-Cake

可触摸且无反馈从不触发触摸事件

  •  0
  • J-Cake  · 技术社区  · 7 年前

    我有下面的React本机代码,它提醒用户它被按下了,但是它没有响应任何东西。

    我试过用不同的方法 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 回复  |  直到 7 年前
        1
  •  0
  •   Jeff Gu Kang    7 年前

    发问者原稿 code .

    解决方案

    由提问者的答案添加。


    换岗: absolute 定位: relative .


    更改样式表生成的样式类型,如下所示。

    你的原件。

    {...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>
        );
      }
    }

    为什么?

    我不确定您如何在其他组件中使用您的组件。但若你们并没有足够大小的容器来显示你们的组件,那个么触摸区域可能不起作用。这就是为什么我有时在测试中使用容器的背景色。


    不可能只通过样式表.create()创建的类似排列运算符访问样式。不要使用StyleSheet.create()或change展平样式表对象。

    风格可以与平台(iOS、Android)不同。

    推荐文章