codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Developing Multi-Slider-Switch in React Native

Victor K Varghese
codeburst
Published in
5 min readFeb 2, 2018

Goal

To create a three-way selectable switch module that can be used to change status of a task. This article aims to shed light into

Implementation

Let’s start with creating a new react-native project

react-native init multiSwitch

Basic Setup

First of all we can create a separate folder named multi switch which will contain all the switch related code. Add an index.js file to export the module, then we can create a file named MultiSwitch.js

Lets give the view a basic style to start with ….

Give it a borderRadius , appropriate height and a subtle background colour. Spice things up by providing a border colour with width:1 .Also add icons that will trigger click events…

Button component
needs to width 1/3rd of the container view

Adding this buttons to our main view

switch render code

Voila … we have a basic layout to play with …yay.. !!

Basic layout

Lets spice things up , shall we ?!

Sliding Switch

We can make use of Animated.View to create an elegant slider icon for the module. This view will work as a switch changing values with a buttery smooth animation!

Lets add an Animated.View

animated switch button added

We can provide styles to the switch including shadow and other style props..

slider style

Finally .. we have our switch button

With animated slider button

PanResponder (The Horror :D )

In React Native, you can use the PanResponder to recognise multi-touch gestures as well as swipes and other touches that make native apps feel snappy and intuitive. But getting it up and running can feel daunting and really hard to achieve.

Once you start understanding the logic behind the responder and familiarise with different methods , it becomes simple and I promise you will die building different types of gestures with it.

The magic starts with

  • onPanResponderGrant - Called when gesture starts
  • onPanResponderMove - Called when the user is dragging
  • onPanResponderRelease — Called when user releases all touches

These following methods enable the pan responder to catch the gestures associated with the corresponding view.

  • onStartShouldSetPanResponder: (evt, gestureState) => true,
  • onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
  • onMoveShouldSetPanResponder: (evt, gestureState) => true,
  • onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,

In a nut shell what we have to do is, Disable parent scroll if any in onPanResponderGrant to avoid scroll issues. Then start updating the position state value and find the final resting position of the slider on onPanResponderRelease then set the state.

gestureState.dx goes on changing
Calculate finalValue based on gestureState and direction

We are moving horizontally so we have to use gestureState.dx to get the value. If its vertical then we have to use gestureState.dy. if (gestureState.dx >0) means we are moving forward and negative then we are going backward.

The code above is absolutely not the best way to calculate positions, But once we have a working prototype we can start generalising everything.

Now we have know each range that corresponds to a section in the slider (1 in 3). Now we can add Animated.timing to support animating the slider to each section when the user taps on each status. Something like below, which will calculate the toValue that animates the slider over a duration the final position

Basic selection logic

With some basic calculations and bug fixes we have a perfectly working slider component now.

Different states in the slider

Lets see the slider in action .. shall we

if you guys want to refer the code please use this GitHub link https://github.com/victorkvarghese/rn-slider-switch

UPDATE: Fixed onStatusChanged not getting called in the lib, Added code to disable switch when needed

PRs are welcome , I will be committing to this library, adding more features.

More articles from me

Hey, I’m looking for remote part-time opportunities

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Android | React Native Dev | Flutter Enthusiast! - Interested in part time opportunities

Responses (5)

Write a response