Skip to content

Instantly share code, notes, and snippets.

@rcerrejon
Created November 13, 2019 08:17
Show Gist options
  • Save rcerrejon/f354f8330e51448c2258beb95a0ee9f6 to your computer and use it in GitHub Desktop.
Save rcerrejon/f354f8330e51448c2258beb95a0ee9f6 to your computer and use it in GitHub Desktop.
swipe React Function
import React, { Component } from 'react'
import { View, Animated, PanResponder } from 'react-native'
// onSwipePerformed = action => {
// switch (action) {
// case 'left': {
// // this.switchTo(1)
// console.log('left Swipe performed')
// break
// }
// case 'right': {
// // this.switchTo(0)
// console.log('right Swipe performed')
// break
// }
// default: {
// console.log('Kein Bewegung')
// }
// }
// }
export default class SwipeGesture extends Component {
componentWillMount = () => {
this.PanResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
let x = gestureState.dx
let y = gestureState.dy
if (Math.abs(x) > Math.abs(y)) {
if (x >= 0) {
this.props.onSwipePerformed('right')
} else {
this.props.onSwipePerformed('left')
}
}
}
})
}
render() {
return (
<Animated.View
{...this.PanResponder.panHandlers}
style={this.props.gestureStyle}
>
<View>{this.props.children}</View>
</Animated.View>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment