react-native-slider icon indicating copy to clipboard operation
react-native-slider copied to clipboard

Move thumb directly where the user taps

Open charliesbot opened this issue 7 years ago • 22 comments

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 avatar Jul 05 '16 17:07 charliesbot

+1

luisfuertes avatar Jul 06 '16 11:07 luisfuertes

+1

hugohow avatar Sep 12 '16 15:09 hugohow

+1

ghost avatar Oct 21 '16 04:10 ghost

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

avkvak avatar Oct 21 '16 13:10 avkvak

+1

jariwalabhavesh avatar May 03 '17 06:05 jariwalabhavesh

avkvak . 666

gitDengXiao avatar Jun 16 '17 03:06 gitDengXiao

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

oliviachang29 avatar Jul 04 '17 16:07 oliviachang29

@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

Ashi90 avatar Jul 10 '17 15:07 Ashi90

@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

avkvak avatar Jul 10 '17 15:07 avkvak

@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.

Ashi90 avatar Jul 11 '17 05:07 Ashi90

@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?

fadlykayo avatar Oct 09 '17 10:10 fadlykayo

@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)

avkvak avatar Oct 09 '17 14:10 avkvak

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

fadlykayo avatar Oct 11 '17 04:10 fadlykayo

@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?

twerff avatar Mar 15 '18 10:03 twerff

@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?

llphickman avatar Sep 18 '18 13:09 llphickman

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
    		...
    	})
    }

saeidee avatar Dec 11 '18 11:12 saeidee

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" />

rtt63 avatar Feb 18 '19 13:02 rtt63

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

rogerkerse avatar May 23 '19 11:05 rogerkerse

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

naveenprakash74 avatar Jun 29 '19 11:06 naveenprakash74

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}>...
  ...);

Sleepful avatar Sep 17 '19 17:09 Sleepful

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

Seems like its not working

RageOfJustice avatar Nov 18 '19 13:11 RageOfJustice

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🙂

naveenprakash74 avatar Dec 08 '19 15:12 naveenprakash74