Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Abdul Wasey
Abdul Wasey

Posted on

     

3D Button in React Native

Intro

In this tutorial, you will learn how to build a basic 3dimensional looking button in react native. We will be utilizing theAnimated module that is readily available in react native.

Lets get started

3D Button


CODE

importReact,{useRef}from'react';import{Text,TouchableWithoutFeedback,Animated}from'react-native';import{tailwind}from'../utils/tailwind';interfaceProps{text?:string;onPress?:()=>void;}constButton:React.FC<Props>=({text,onPress})=>{constanimation=useRef(newAnimated.Value(0)).current;consthandlePress=()=>{Animated.timing(animation,{toValue:1,duration:50,useNativeDriver:true,}).start();};consthandleButtonOut=()=>{Animated.timing(animation,{toValue:0,duration:50,useNativeDriver:true,}).start();if(onPress){onPress();}};return(<TouchableWithoutFeedbackonPressIn={handlePress}onPressOut={handleButtonOut}><Viewstyle={tailwind('rounded-full','bg-rose-700','border-rose-700')}><Animated.Viewstyle={[tailwind('rounded-full','items-center','bg-red-500','border','border-red-500',),{transform:[{translateY:animation.interpolate({inputRange:[0,1],outputRange:[-5,0],}),},],},]}><Textstyle={tailwind('font-bold','text-white','text-lg')}>{text}</Text></Animated.View></View></TouchableWithoutFeedback>);};exportdefaultButton;
Enter fullscreen modeExit fullscreen mode

Explanation

Declare a animation variable and assign the value to 0. We need wrap it in the useRef hook to make sure the animation value persists any changes that might happen outside the 3D button component.

constanimation=useRef(newAnimated.Value(0)).current;
Enter fullscreen modeExit fullscreen mode

The press animation starts on clicking the button where theinterpolate function is invoked changing the value from0 to 1, this causes thetranslateY value to change from-5px to 0px

ValuetranslateY
0-5px
10px

The above table shows how the value relates to the pixels changes, you can add more values and customize it.

transform:[{translateY:animation.interpolate({inputRange:[0,1],outputRange:[-5,0],})}]
Enter fullscreen modeExit fullscreen mode

onPressIn triggers the function to change theanimation variable value to 1, we can assign a duration value of50 orany number.

Animated.timing(animation,{toValue:1,duration:50,useNativeDriver:true,}).start();
Enter fullscreen modeExit fullscreen mode

onPressOut function is triggered when the user presses out of theTouchableWithoutFeedback where the value goes back to 0

Animated.timing(animation,{toValue:0,duration:50,useNativeDriver:true,}).start();
Enter fullscreen modeExit fullscreen mode

Hope you enjoyed this mini tutorial.
Abdul Wasey

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I make things happen.Dream big and follow through even bigger.
  • Location
    Hyderabad, IN
  • Work
    Full Stack Engineer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp