Skip to content

Instantly share code, notes, and snippets.

@gHashTag
Created January 6, 2018 12:56
Show Gist options
  • Save gHashTag/09ccd2e6c44049aadfe1d62837f444ea to your computer and use it in GitHub Desktop.
Save gHashTag/09ccd2e6c44049aadfe1d62837f444ea to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import {
StyleSheet,
Image, // we want to use an image
PanResponder, // we want to bring in the PanResponder system
Animated // we wil be using animated value
} from 'react-native';
export default class Briliant extends Component {
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY(),
scale: new Animated.Value(1)
};
}
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderGrant: (e, gestureState) => {
// Set the initial value to the current state
this.state.pan.setOffset({x: this.state.pan.x._value, y: this.state.pan.y._value});
this.state.pan.setValue({x: 0, y: 0});
Animated.spring(
this.state.scale,
{ toValue: 1.1, friction: 3 }
).start();
},
// When we drag/pan the object, set the delate to the states pan position
onPanResponderMove: Animated.event([
null, {dx: this.state.pan.x, dy: this.state.pan.y},
]),
onPanResponderRelease: (e, {vx, vy}) => {
// Flatten the offset to avoid erratic behavior
this.state.pan.flattenOffset();
Animated.spring(
this.state.scale,
{ toValue: 1, friction: 3 }
).start();
}
});
}
render() {
// Destructure the value of pan from the state
let { pan, scale } = this.state;
// Calculate the x and y transform from the pan value
let [translateX, translateY] = [pan.x, pan.y];
let rotate = '0deg';
// Calculate the transform property and set it as a value for our style which we add below to the Animated.View component
let imageStyle = {transform: [{translateX}, {translateY}, {rotate}, {scale}]};
return (
<Animated.View style={imageStyle} {...this._panResponder.panHandlers}>
<Image style={styles.gems} source={require('./../media/gems/briliant.png')} />
</Animated.View>
);
}
}
const styles = StyleSheet.create({
gems: {
width: 38,
height: 38
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment