【问题标题】:React-Native: How to make image arrayReact-Native:如何制作图像数组
【发布时间】:2017-04-05 19:08:27
【问题描述】:

我有一个屏幕,我想在其上显示 4 张图像。

我怎样才能做到这一点,以便当我单击图像时,它会转到数组中的下一个(我想这就是它的制作方式)。除了触摸之外,我还希望有按钮可以转到下一张图像或返回上一张图像。

我希望有人可以帮助我解决这个问题。我不知道如何处理它。我想找到实现它的最佳方法。

谢谢!

更新: 感谢 Codesingh 的回答,一切正常!

【问题讨论】:

    标签: react-native reactive-programming react-native-ios react-android react-native-image


    【解决方案1】:

    我正在考虑您有 3 张图片。

    请看一下:

        var arr=[{image:require('image1')},{image:require('image2')},{image:require('image3')}];
    
        Class CoolSlider extends component{
    
        constructor(props)
        {
          super(props);
          this.state = {
            counter:0,
          }
        }
    
        changeImage(button)
        {
          if(button == 1)
          {
            this.setState({counter: this.state.counter + 1})
          }
          else
          {
            this.setState({counter: this.state.counter - 1}) 
          }
    
        }
    
        render()
        { 
    
          let button1 = false,button2 = false; 
         if(this.state.counter == 0)
          {
            //make the previous button disable
             button2=true;
          } 
    
         if(this.state.counter == 2)
         {
             button1=true;
         } 
    
        //     if(arr.length == 1 )
        //    {
        //        button1=true;
        //        button2=true;  
        //    }
    return (
        <View>
        <Image source = {arr[this.state.counter].image} />
        <TouchableHighlight disabled={button1} onPress = this.changeImage(1)>
        <Text>
          Next
        </Text>
        </TouchableHighlight>
    
        <TouchableHighlight disabled={button2} onPress = this.changeImage(2)>
        <Text>
          Previous
        </Text>
        </TouchableHighlight>
    )
        }
        }
    

    编辑在这里:

    changeImage(button) {
        if(button == 1) {
          //if you reach the end i.e below statement then counter 0
          if (this.state.counter == arr.length - 1) this.setState({counter: 0})
          else this.setState({counter: this.state.counter + 1})
        }
        //similarly if you reach the starting then counter will be at end of the 
        array 
        else {
          if (this.state.counter == 0) this.setState({counter: arr.length - 1})
          else this.setState({counter: this.state.counter - 1}) 
        }
      }
    

    干杯:)

    【讨论】:

    • 快速提问。我试着让它像 Rob 在你下面建议的那样循环,但我似乎无法让它工作你知道怎么做吗?我没有收到错误,它只是不起作用。我希望能够只按加号按钮和图像循环。其他一切都按照您的方式完美运行。谢谢!
    • 当你按下加号按钮时发生了什么?
    • 其实没什么。只有减号按钮转到上一张图像。但加号没有任何作用。如果我像下面的 Rob 那样做。此外,如果我在到达最后一张图像后按减号,它就不会循环。所以应用程序启动。我可以单击两次减号按钮,然后什么也没有。
    • 问题出在这里: if(button == 1) { if (this.state.counter == arr.length - 1) this.setState({counter: 0}) else this.setState ({counter: this.state.counter + 1}) }
    • 每当他的代码中的 this.state.counter 到达数组的末尾,然后将状态更改为 0 .. 但是 this.state.counter 永远不会到达,因为它没有增加,它会得到仅当它进入 cmets 中的上述 if 语句并且它永远不会到达内部时才会增加,因此按钮什么也不做:)
    【解决方案2】:

    您也可以通过对@Codesingh 的答案进行小幅修改来使其成为无限循环。我还将其更改为不特定于您拥有的图像数量,并根据推荐的 JS 编码标准对其进行正确格式化:

    (注意:未经测试)

    const arr = [require('image1'), require('image2'), require('image3')];
    
    Class CoolSlider extends component {
    
      constructor(props) {
        super(props);
        this.state = {
          counter: 0,
        }
      }
    
      changeImage(button) {
        if(button == 1) {
          if (this.state.counter == arr.length - 1) this.setState({counter: 0})
          else this.setState({counter: this.state.counter + 1})
        } else {
          if (this.state.counter == 0) this.setState({counter: arr.length - 1})
          else this.setState({counter: this.state.counter - 1}) 
        }
      }
    
      render() {
        let button1 = false,button2 = false; 
        if(this.state.counter == 0) {
          //make the previous button disable
          button2=true;
        } 
    
        if(this.state.counter == (arr.length - 1)) {
          button1=true;
        } 
    
    
        return (
          <View>
            <Image source = {arr[this.state.counter]} />
            <TouchableHighlight disabled={button1} onPress = this.changeImage(1)>
              <Text>
                Next
              </Text>
            </TouchableHighlight>
    
            <TouchableHighlight disabled={button2} onPress = this.changeImage(2)>
              <Text>
                Previous
              </Text>
            </TouchableHighlight>
          </View>
        )
      }
    }
    

    【讨论】:

    • 感谢您的回答,但如果可能的话,我有一个小问题,请检查一下。我已经更新了我的帖子。非常感谢!
    • 请使用您正在使用的不同版本进行更新 - 您可以使用以下命令找到 react native 版本:react-native -v。另外,您是否遇到任何错误?
    • 对不起!这些是我使用的 react 版本: react-native-cli: 2.0.1 react-native: 0.42.0 如果我启用远程调试,我只会得到错误。否则只是一个白屏,大约 30 秒后,应用程序停止响应并崩溃。我在错误的主要帖子中添加了两个屏幕截图。完整的太长了。
    猜你喜欢
    • 2018-10-24
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多