Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move thumb directly where the user taps #45

Open
charliesbot opened this issue Jul 5, 2016 · 22 comments
Open

Move thumb directly where the user taps #45

charliesbot opened this issue Jul 5, 2016 · 22 comments

Comments

@charliesbot
Copy link

charliesbot commented Jul 5, 2016

Only way to move the thumb is by sliding it. But if the user taps directly in a section of the slider, it won't move directly to that position.

I've seen that behaviour in the youtube app, for example

so far, what a great library!

@charliesbot charliesbot changed the title Move thumb directly where the user tap Move thumb directly where the user taps Jul 5, 2016
@luisfuertes
Copy link

+1

2 similar comments
@hugohow
Copy link

hugohow commented Sep 12, 2016

+1

@ghost
Copy link

ghost commented Oct 21, 2016

+1

@avkvak
Copy link

avkvak commented Oct 21, 2016

you can just wrap it with TouchableWithoutFeedback and gets coordinates when user tap.

tapSliderHandler = (evt) => { this.refs.slider.measure((fx, fy, width, height, px, py) => { this.setState({value: (evt.nativeEvent.locationX - px) / width}); })); }

<View ref="slider"> <TouchableWithoutFeedback onPressIn={this.tapSliderHandler}> <Slider value={this.state.value}/> </TouchableWithoutFeedback> </View>

it works for me

@jariwalabhavesh
Copy link

+1

@gitDengXiao
Copy link

avkvak . 666

@oliviachang29
Copy link

avkvak's solution works well - hoping you could put it into a PR?

@Ashi90
Copy link

Ashi90 commented Jul 10, 2017

@avkvak , can you please let me know how it's work,
For me it always provide '0', I got following values
evt.nativeEvent.locationX = 61.17 (changeable on touch)
px = 0(always)
with = 360 (always)

So according to formula (evt.nativeEvent.locationX - px) / width
(61.17 - 0) / 360

@avkvak
Copy link

avkvak commented Jul 10, 2017

@Ashi90 , "px" is a just slider offset from the edge of the screen.
So we calculate exactly the part of the pressed slider, except white space

@Ashi90
Copy link

Ashi90 commented Jul 11, 2017

@avkvak ,Ok "PX" is offset value from the edge of the screen, but what about others, I'm not getting the actual position of touch in slider.

@fadlykayo
Copy link

@Ashi90 have the same problem. not getting the actual position where I tap. did you already find any solution for this?

@avkvak could you maybe explain more about the formula? why you divide it with width?

@avkvak
Copy link

avkvak commented Oct 9, 2017

@fadlykayo because react-native-slider takes its value from 0 (left edge) to 1 (right edge).
therefore we need to find the percentage of the completed area.
If user tapped on 61.16px from the edge of the slider with 360px width, it means that he pressed on the 0.17 part of the slider, so we can set the state of slider value

and yes, sorry for my english)

@fadlykayo
Copy link

@avkvak thank you for the detailed explanation. hope this also could help others.

@twerff
Copy link

twerff commented Mar 15, 2018

@avkvak Thank you for this tip. This works great! Next up is to figure out how the the thumb will continue to be dragged after the user pressed the slider... Any ideas?

@llphickman
Copy link

@avkvak Thank you for this tip. This works great! Next up is to figure out how the the thumb will continue to be dragged after the user pressed the slider... Any ideas?

Any progress on this?

@saeidee
Copy link

saeidee commented Dec 11, 2018

You can do something like this!

handelSliderTap = (event) => { 
    	this.refs.sliderWrapper.measure((fx, fy, width, height, px, py) => {
    		const completedDistance = ((event.nativeEvent.locationX - px) / width) * 100
                const completedDuration = (completedDistance * this.state.duration) / 100
    		...
    	})
    }

@rtt63
Copy link

rtt63 commented Feb 18, 2019

Sadly this workaround causes an error on android to me
screen shot 2019-02-18 at 17 15 43

upd: it can be fixed by adding any style to <View ref="slider" />

@rogerkerse
Copy link

It would be nice if it was built in and after pressing somewhere you could immediately start moving your finger to position slider more precisely

@naveenprakash74
Copy link

have look on pull request #164 it will be helpful for you

@Sleepful
Copy link

Sleepful commented Sep 17, 2019

here is @avkvak answer but with hooks, works by tapping, but not by tapping and dragging in the same gesture

const AddSlider = ({ setState, state }) => {
  const viewRef = useRef(null);
  const tapSliderHandler = (evt) => {
    if (viewRef.current) {
      viewRef.current.measure((fx, fy, width, height, px) => {
        const location = ((evt.nativeEvent.locationX - px) / width) * 100;
        setState(location);
      });
    }
  };
  return (
      <View ref={ viewRef }>
        <TouchableWithoutFeedback onPressIn={ tapSliderHandler }>
           <Slider value={state}>...
  ...);

@RageOfJustice
Copy link

have look on pull request #164 it will be helpful for you

Seems like its not working

@naveenprakash74
Copy link

have look on pull request #164 it will be helpful for you

Seems like its not working
If you are facing then comment i will try to resolve it🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests