【问题标题】:Make a background Image disappear from button on button click, TouchableWithoutFeedback - React Native?使背景图像从按钮单击按钮上消失,TouchableWithoutFeedback - React Native?
【发布时间】:2018-10-03 20:45:51
【问题描述】:

我创建了一个非常大的按钮,上面有一个长颈鹿头和一个蓝天,后面有云。我想知道当你点击长颈鹿的头部时,如何让图像(背后的天空)消失,然后当你再次点击长颈鹿时重新出现。我需要很多这样的按钮,所以我尝试制作一个可重复使用的按钮组件。

我制作了一堆组件。 https://snack.expo.io/@tamsynjennifer/custom-button

否则这是代码:

可重用按钮.JS

import React from 'react';
import { View, StyleSheet, Image, TouchableWithoutFeedback } from 'react-native';

const Button = ({ backgroundImage, mainImage, onPress }) => (
  <View style={styles.container}>
    <Image
       style={styles.bgImage}
       source={backgroundImage}
       resizeMode={'contain'}
    />
    <TouchableWithoutFeedback 
      onPress={onPress}
      style={styles.button}
    >
     <Image
        style={styles.image}
        source={mainImage}
        resizeMode={'contain'}
     />
    </TouchableWithoutFeedback>
  </View>
);

const styles = StyleSheet.create({
  container: {
    height: 250,
    width: 250,
    justifyContent: 'center',
    alignItems: 'center',
    marginTop: 0,
  },
  button: {
    height: 200,
    width: 200
  },
  bgImage: {
    alignSelf: 'center',
    justifyContent: 'center',
    position: 'absolute',
    height: 250,
    width: 250,
  },
  image: {
   alignSelf: 'center',
    justifyContent: 'center',
    position: 'absolute',
    height: 200,
    width: 200
  },
});

export default Button;

APP.JS

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

const bgImage = require("./assets/BgImage.png");
const mainImage = require("./assets/MainImage.png");

import Button from './Button';

class App extends Component {
  render() {
    return (
      <View style={styles.container}>
          <View style={styles.button}>
             <Button
                backgroundImage={bgImage}
                mainImage={mainImage}
             />
          </View>

      </View>
     );
   }
}

const styles = StyleSheet.create({
  container: {
    height: 300,
    width: 300,
    justifyContent: 'center',
    alignItems: 'center'
  },
  button: {
    height: 250,
    width: 250,
    alignSelf: 'center',
    position: 'absolute' 
  },
});

export default App;

【问题讨论】:

    标签: react-native button touchablewithoutfeedback


    【解决方案1】:

    这是你可以做到的,我已经修复了你的代码。首先你需要在onPress上设置状态和改变状态,以便组件重新渲染和改变图像。只需用这个替换你的类。 Expo Link

    import React, { Component } from 'react';
    import { Text, View, StyleSheet } from 'react-native';
    import { Constants } from 'expo';
    
    const bgImage = require("./assets/BgImage.png");
    const mainImage = require("./assets/MainImage.png");
    
    import Button from './Button';
    
    class App extends Component {
    constructor(props){
    super(props)
    this.state = {
      imageVisibility: true,
      backgroundImagePath: require("./assets/BgImage.png")
    }
    }
    
      render() {
        return (
          <View style={styles.container}>
              <View style={styles.button}>
                 <Button
                    backgroundImage={this.state.backgroundImagePath}
                    mainImage={mainImage}
                    onPress= {() => {this.changeImgaeVisibility()}}
                 />
              </View>
    
          </View>
         );
       }
    
       changeImgaeVisibility = () => {
       if(this.state.imageVisibility){
           this.setState({imageVisibility: false, backgroundImagePath: null})
       }else{
           this.setState({imageVisibility: true, backgroundImagePath: require("./assets/BgImage.png")})
       }
       }
    
       }
    
    const styles = StyleSheet.create({
      container: {
        height: 300,
        width: 300,
        justifyContent: 'center',
        alignItems: 'center'
      },
      button: {
        height: 250,
        width: 250,
        alignSelf: 'center',
        position: 'absolute' 
      },
    });
    
    export default App;
    

    【讨论】:

    • 是的,它确实有效,尽管因为我需要很多这些按钮,所以当它们都在同一个屏幕上时,它会成为一个问题——当你点击一个时,它们都是可见的或不可见的。 snack.expo.io/@tamsynjennifer/custom-button-2 所以我想我把你的概念放在按钮组件中,而不是让它可重复使用,并在同一个屏幕上有多个按钮。 snack.expo.io/@tamsynjennifer/custom-button-2_separate-clicks
    • 也许有一种非常简单和更好的方法来解决屏幕上的按钮超过 1 个的问题?我是新来的,所以我不确定我所做的是否是最佳实践?我也知道初始代码没有两个按钮。非常感谢您的帮助!
    • snack.expo.io/@tamsynjennifer/custom-button-2_separate-clicks 这适用于在同一屏幕上有两个按钮。
    • 您是否阅读了我上面的 cmets 并查看了其他两个零食示例?
    • 代码看起来不错,您在Button组件中实现了状态更改,单独调用按钮会生成不同的对象,它会正常工作。
    【解决方案2】:

    您可以将一个函数作为 javascript 对象添加到您的 JSX 以渲染背景图像,此方法将返回您需要渲染的对象,如下例所示:

    const handleBackgroundImg = (imageBg, backgroundiImage) => {
      if (imageBg === true) {
       return <Image style={styles.bgImage} source={backgroundImage} resizeMode={'contain'}/>
      }
      return <Image />;
    };
    

    要将此函数添加到您的 JSX 代码中,您必须像这样进行更改:

    const Button = ({ backgroundImage, mainImage, onPress, imageBg }) => (
      <View style={styles.container}>
        {handleBackgroundImg(imageBg, backgroundImage)}
        <TouchableWithoutFeedback 
          onPress={onPress}
          style={styles.button}
        >
         <Image
            style={styles.image}
            source={mainImage}
            resizeMode={'contain'}
         />
        </TouchableWithoutFeedback>
      </View>
    );
    

    现在,当您使用此组件时,您还必须传递 imageBg 布尔值,当您想要显示 bg 时考虑 true,而当您想要隐藏它时考虑 false。请看下面的代码:

    <Button
      backgroundImage={bgImage}
      mainImage={mainImage}
      imageBg={true}
    />
    

    如果您有任何疑问,请在此处再次询问,我可以为您提供帮助。

    【讨论】:

    • 感谢您抽出宝贵时间回复。我无法让你的例子起作用。我收到错误“找不到变量:backgroundImage”
    • 我编辑了代码来解决这个问题,我忘记将你的背景作为参数传递给 handleBackgroundImg 函数。我看到您已经使用了另一种方法来解决问题,但是如果您想尝试不同的解决方案,希望它可以提供帮助。
    • 我试过了,现在没有错误,但是当我点击长颈鹿时没有任何反应。背景不会消失。对你起作用吗?尽管上面还有另一种解决方案,但不要太担心。还是谢谢你!
    • 这可能是因为在您的 onPress 函数中您没有更改应用程序的状态。每次必须重新渲染屏幕中的某些内容时,您都必须更改状态,因此将再次调用渲染方法。
    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2017-03-07
    • 2013-04-25
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多