53
loading...
This website collects cookies to deliver better user experience
import { StyleSheet, Text, View, Image, Animated } from 'react-native';
const translateX = new Animated.Value(0)
<Animated.View style={{backgroundColor:"yellow", width:"75%", height:"75%", transform:[{translateX}]}}>
<Image source={profile.pic}></Image>
<Text>{profile.name}</Text>
<Text>Age: {profile.age}</Text>
<Text>Likes: {profile.likes.join(', ')}</Text>
</Animated.View>
<PanGestureHandler
onHandlerStateChange={handleSwipe}
onGestureEvent={handlePan}
>
<Animated.View style={{backgroundColor:"yellow", width:"75%", height:"75%", transform:[{translateX}]}}>
<Image source={profile.pic}></Image>
<Text>{profile.name}</Text>
<Text>Age: {profile.age}</Text>
<Text>Likes: {profile.likes.join(', ')}</Text>
</Animated.View>
</PanGestureHandler>
const handlePan= Animated.event(
[{nativeEvent:{translationX:translateX}}],{useNativeDriver:true}
)
const reset = Animated.timing(translateX,{
toValue:0,
duration:250,
useNativeDriver:true
})
const swipeRightAnimation = Animated.timing(translateX,{
toValue: 600,
duration: 400,
useNativeDriver:true
})
const swipeLeftAnimation = Animated.timing(translateX,{
toValue: -600,
duration: 400,
useNativeDriver:true
})
const handleSwipe=({nativeEvent}) =>{
const {state} = nativeEvent
if(state === 5){
//handle swipe right
if(nativeEvent.translationX < -225){
index++
swipeRightAnimation.start(()=>{
//add profile to match list
setProfile(profiles[index%3])
})
}
//handle swipe left
else if(nativeEvent.translationX > 225){
index++
swipeLeftAnimation.start(()=>{
setProfile(profiles[index%3])
})
}
//handle uncompleted swipe
else reset.start()
}
}