React Native-如何在onPress中更改视图的样式和图像

约翰

我有三个TouchableHighlight元素,它们包装了三个视图(彼此并排排列)。Onpress我想更改视图的样式(backgroundColor)和图像(按下的视图将变为活动状态)。

  • 活动视图<View style={styles.circle}>-backgroundColor应该变成“红色”,图像源应该是“ arrow-win-active.png”<Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
  • 其他两个视图保持不变

最好的方法是什么?

这是屏幕截图:

在此处输入图片说明

到目前为止,这是我的代码:

import React from 'react'
import {
    View,
    ListView,
    ScrollView,
    StyleSheet,
    Image,
    TouchableHighlight,
} from 'react-native'

const changeStyle = () => {
    console.log('change style')
}

const appView = (game, date) =>
<ScrollView style={styles.container}>
    <View style={styles.step}>
        <View style={{flex:1}}>
            <View style={styles.pickContainer}>
                <TouchableHighlight onPress={() => changeStyle()} style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
                        </View>
                    </View>
                </TouchableHighlight>

                <TouchableHighlight style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-draw.png')} style={styles.arrowDraw}></Image>
                        </View>
                    </View>
                </TouchableHighlight>

                <TouchableHighlight style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
                        </View>
                    </View>
                </TouchableHighlight>
            </View>
        </View>
    </View>
</ScrollView>

const styles = StyleSheet.create({
container: {
    flex: 1,
    backgroundColor: '#e1e1e1'
},
step: {
    backgroundColor: '#ffffff',
    borderRadius: 4,
    borderLeftWidth: 5,
    flex: 1,
    marginLeft: 10,
    marginRight: 10,
    marginBottom: 10,
    paddingLeft: 15,
    paddingRight: 10,
    paddingTop: 15,
    paddingBottom: 15,
    shadowOffset: {
        width: 0,
        height: 2,
    },
    shadowRadius: 2,
    shadowOpacity: 0.2,
    shadowColor: 'black',
    textAlign: 'center',
},
heading: {
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 15,
    color: '#333333',
},
pickContainer: {
    flex:1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
},
pickWrapper: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    marginTop: 10,
},
circle: {
    height: 60,
    borderRadius: 30,
    width: 60,
    backgroundColor: '#eeeeee',
    alignItems: 'center',
    justifyContent: 'center',
},
arrowWin: {
    width: 34,
    height: 28,
},
arrowDraw: {
    width: 18,
    height: 8,
},
})

export default appView
亨里克·R

您必须更改AppView为基于类的组件,因为您必须访问state

import React. {Component} from 'react'
import {
  View,
  ListView,
  ScrollView,
  StyleSheet,
  Image,
  TouchableHighlight,
} from 'react-native'

class AppView extends Component {
  state = {
    isPlayer1ButtonActive: false,
    isDrawButtonActive: false,
    isPlayer2ButtonActive: false,
  }

  activateButton = buttonToActivate => {
    const newState = Object.assign(
      {}, 
      {
        isPlayer1ButtonActive: false,
        isDrawButtonActive: false,
        isPlayer2ButtonActive: false,
      }, 
      {[buttonToActivate]: true},
    )
    this.setState(newState);
  }

  render() {
    const {isPlayer1ButtonActive, isDrawButtonActive, isPlayer2ButtonActive} = this.state

    return (
      <ScrollView style={styles.container}>
        <View style={styles.step}>
          <View style={{flex:1}}>
            <View style={styles.pickContainer}>
              <TouchableHighlight onPress={() => activateButton('isPlayer1ButtonActive')} style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isPlayer1ButtonActive && styles.circleActive]}>
                    <Image 
                      source={isPlayer1ButtonActive ? require('../images/arrow-win-active.png') : require('../images/arrow-win.png')} 
                      style={styles.arrowWin} 
                    />
                  </View>
                </View>
              </TouchableHighlight>

              <TouchableHighlight onPress={() => activateButton('isDrawButtonActive')}  style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isDrawButtonActive && styles.circleActive]}>
                    <Image 
                      source={isDrawButtonActive ? require('../images/arrow-draw-active.png') : require('../images/arrow-draw.png')} 
                      style={styles.arrowDraw} 
                    />
                  </View>
                </View>
              </TouchableHighlight>

              <TouchableHighlight onPress={() => activateButton('isPlayer2ButtonActive')}  style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isPlayer2ButtonActive && styles.circleActive]}>
                    <Image 
                      source={isPlayer2ButtonActive ? require('../images/arrow-win-active.png') : require('../images/arrow-win.png')} 
                      style={styles.arrowWin} 
                    />
                  </View>
                </View>
              </TouchableHighlight>
            </View>
          </View>
        </View>
      </ScrollView>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#e1e1e1'
  },
  step: {
    backgroundColor: '#ffffff',
    borderRadius: 4,
    borderLeftWidth: 5,
    flex: 1,
    marginLeft: 10,
    marginRight: 10,
    marginBottom: 10,
    paddingLeft: 15,
    paddingRight: 10,
    paddingTop: 15,
    paddingBottom: 15,
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowRadius: 2,
    shadowOpacity: 0.2,
    shadowColor: 'black',
    textAlign: 'center',
  },
  heading: {
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 15,
    color: '#333333',
  },
  pickContainer: {
    flex:1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  pickWrapper: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    marginTop: 10,
  },
  circle: {
    height: 60,
    borderRadius: 30,
    width: 60,
    backgroundColor: '#eeeeee',
    alignItems: 'center',
    justifyContent: 'center',
  },
  circleActive: {
    backgroundColor: 'red',
  },
  arrowWin: {
    width: 34,
    height: 28,
  },
  arrowDraw: {
    width: 18,
    height: 8,
  },
})

export default AppView

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Native:如何在onPress事件上更改视图

来自分类常见问题

如何在React Native中更改特定列的样式?

来自分类Dev

如何在React Native中更改TextInput占位符的样式?

来自分类Dev

如何在React Native中更改语句中的单词样式?

来自分类Dev

如何在React Native中优化图像

来自分类Dev

如何在React Native中优化图像

来自分类Dev

如何在 React Native 中缩放图像?

来自分类Dev

OnPress从循环更改组件样式-带钩子的React Native

来自分类Dev

使用 React Native 在 onPress 后更改按钮样式

来自分类Dev

如何在React Native中以16:9的比例设置视图样式?

来自分类Dev

如何在 React Native 的视图中将图像居中

来自分类Dev

在React Native中按更改按钮样式

来自分类Dev

如何从 React Native 的列表中更改所选文本的样式

来自分类Dev

如何在React Native中更改子字符串样式

来自分类Dev

在React Native中更改父视图的道具

来自分类常见问题

如何在React Native中设置TextInput的样式以输入密码

来自分类Dev

如何在React Native中添加按钮网格样式?

来自分类Dev

如何在 React Native 中通过点击设置动态样式?

来自分类Dev

如何在react-native中组合多个内联样式对象和内联css?

来自分类Dev

React Native 动态样式

来自分类Dev

如何在React Native中安装React Native Maps?

来自分类Dev

如何在React Native中处理图像序列

来自分类Dev

如何在React native中动态设置图像的来源?

来自分类Dev

如何在React Native中重复图案图像以创建背景?

来自分类Dev

如何在React Native中自动缩放图像?

来自分类Dev

如何在React Native中为图像添加标题

来自分类Dev

如何在 React Native 中显示渐变 SVG 图像

来自分类Dev

如何在 React Native 中引用启动图像

来自分类Dev

如何在 React Native 中显示 https 图像?

Related 相关文章

  1. 1

    React Native:如何在onPress事件上更改视图

  2. 2

    如何在React Native中更改特定列的样式?

  3. 3

    如何在React Native中更改TextInput占位符的样式?

  4. 4

    如何在React Native中更改语句中的单词样式?

  5. 5

    如何在React Native中优化图像

  6. 6

    如何在React Native中优化图像

  7. 7

    如何在 React Native 中缩放图像?

  8. 8

    OnPress从循环更改组件样式-带钩子的React Native

  9. 9

    使用 React Native 在 onPress 后更改按钮样式

  10. 10

    如何在React Native中以16:9的比例设置视图样式?

  11. 11

    如何在 React Native 的视图中将图像居中

  12. 12

    在React Native中按更改按钮样式

  13. 13

    如何从 React Native 的列表中更改所选文本的样式

  14. 14

    如何在React Native中更改子字符串样式

  15. 15

    在React Native中更改父视图的道具

  16. 16

    如何在React Native中设置TextInput的样式以输入密码

  17. 17

    如何在React Native中添加按钮网格样式?

  18. 18

    如何在 React Native 中通过点击设置动态样式?

  19. 19

    如何在react-native中组合多个内联样式对象和内联css?

  20. 20

    React Native 动态样式

  21. 21

    如何在React Native中安装React Native Maps?

  22. 22

    如何在React Native中处理图像序列

  23. 23

    如何在React native中动态设置图像的来源?

  24. 24

    如何在React Native中重复图案图像以创建背景?

  25. 25

    如何在React Native中自动缩放图像?

  26. 26

    如何在React Native中为图像添加标题

  27. 27

    如何在 React Native 中显示渐变 SVG 图像

  28. 28

    如何在 React Native 中引用启动图像

  29. 29

    如何在 React Native 中显示 https 图像?

热门标签

归档