0

I have a scroll view that contains an image that covers the entire screen. There are two buttons at the bottom of the screen, these will allow you to download or set the image as a wallpaper. The problem I'm having is I don't know which image is being looked at in the horizontal scroll view when the button is clicked.

I have knowledge of the screen size and was assuming if I could get the content offset or something like that from the currently viewed image in the view divided by the screen size could let me know what image in the array I'm looking at to perform the rest of the functionality.

Unfortunately, I'm unaware of how this could be done the code is below to give a bit of context.

        <ScrollView 
          horizontal
          decelerationRate={0}
          snapToInterval={width}
          snapToAlignment={"center"}
          style={{ height: height }}
        >
          {
            availibleBackgrounds.map(element => (
              <Image
                style={{ width: width, height: height, justifyContent: 'flex-end', alignItems: 'center' }}
                source={{
                  uri: element
                }}
              />
            ))
          }
        </ScrollView>

2 Answers 2

1

You need to get the offset of the content and you can do it with adding the following prop the the ScrollView:

onMomentumScrollEnd={event => setSelectedIndex(event.nativeEvent.contentOffset.x/width)}

setSelectedIndex is a function you should create.

Sign up to request clarification or add additional context in comments.

4 Comments

Once I get things started I will give this a try cheers
So i was having a play around with this solution and im guessing its going to require quite a lot of calculations to determine as the snap value isnt given to me its the end drag value thats givent to me but sometimes it can scrollback snap or scroll forward snap and you dont really know which way it has went as the value just tells you where you liftwed the finger.
Swap the 'onScrollEndDrag' with 'onMomentumScrollEnd'. Let me know if it worked so I'll edit my answer (for future generations ;)
Was just about to through that ion here it's much better on the calculations I have using the onMomentumScrollEnd method so i would suggest the change to onMomentumScrollEnd instead of the dragEnd
1

To add a little more closure to this question here's what I've done

  calculateCurrentIndex(position) {
    this.setState({ 
      selectedIndex: {
        current: Math.ceil((position / width).toFixed(2)),
      } 
    });
  }

----------------------------------------

        <ScrollView 
          horizontal
          decelerationRate={0}
          snapToInterval={width}
          snapToAlignment={"center"}
          style={{ height: height }}
          onMomentumScrollEnd={event => this.calculateCurrentIndex(event.nativeEvent.contentOffset.x)}
          showsHorizontalScrollIndicator={false}
        >
          {
            availibleBackgrounds.map((element, index) => (
              <Image
                key={index}
                style={{ width: width, height: height, justifyContent: 'flex-end', alignItems: 'center' }}
                source={{
                  uri: element
                }}
              />
            ))
          }
        </ScrollView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.