react-native/RNTester/js/PanResponderExample.js

134 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-03-24 18:31:07 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow weak
2015-03-24 18:31:07 +00:00
*/
2015-03-24 18:31:07 +00:00
'use strict';
var React = require('react');
var createReactClass = require('create-react-class');
var ReactNative = require('react-native');
var {PanResponder, StyleSheet, View} = ReactNative;
2015-03-24 18:31:07 +00:00
var CIRCLE_SIZE = 80;
var PanResponderExample = createReactClass({
displayName: 'PanResponderExample',
2015-03-24 18:31:07 +00:00
statics: {
title: 'PanResponder Sample',
description:
'Shows the use of PanResponder to provide basic gesture handling.',
2015-03-24 18:31:07 +00:00
},
_panResponder: {},
_previousLeft: 0,
_previousTop: 0,
_circleStyles: {},
circle: (null: ?{setNativeProps(props: Object): void}),
2015-03-24 18:31:07 +00:00
UNSAFE_componentWillMount: function() {
2015-03-24 18:31:07 +00:00
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminate: this._handlePanResponderEnd,
});
this._previousLeft = 20;
this._previousTop = 84;
this._circleStyles = {
style: {
left: this._previousLeft,
top: this._previousTop,
backgroundColor: 'green',
},
2015-03-24 18:31:07 +00:00
};
},
componentDidMount: function() {
this._updateNativeStyles();
2015-03-24 18:31:07 +00:00
},
render: function() {
return (
<View style={styles.container}>
2015-03-24 18:31:07 +00:00
<View
ref={circle => {
2015-03-24 18:31:07 +00:00
this.circle = circle;
}}
style={styles.circle}
{...this._panResponder.panHandlers}
/>
</View>
);
},
_highlight: function() {
this._circleStyles.style.backgroundColor = 'blue';
this._updateNativeStyles();
2015-03-24 18:31:07 +00:00
},
_unHighlight: function() {
this._circleStyles.style.backgroundColor = 'green';
this._updateNativeStyles();
2015-03-24 18:31:07 +00:00
},
_updateNativeStyles: function() {
2015-03-24 18:31:07 +00:00
this.circle && this.circle.setNativeProps(this._circleStyles);
},
_handleStartShouldSetPanResponder: function(
e: Object,
gestureState: Object,
): boolean {
2015-03-24 18:31:07 +00:00
// Should we become active when the user presses down on the circle?
return true;
},
_handleMoveShouldSetPanResponder: function(
e: Object,
gestureState: Object,
): boolean {
2015-03-24 18:31:07 +00:00
// Should we become active when the user moves a touch over the circle?
return true;
},
_handlePanResponderGrant: function(e: Object, gestureState: Object) {
this._highlight();
},
_handlePanResponderMove: function(e: Object, gestureState: Object) {
this._circleStyles.style.left = this._previousLeft + gestureState.dx;
this._circleStyles.style.top = this._previousTop + gestureState.dy;
this._updateNativeStyles();
2015-03-24 18:31:07 +00:00
},
_handlePanResponderEnd: function(e: Object, gestureState: Object) {
this._unHighlight();
this._previousLeft += gestureState.dx;
this._previousTop += gestureState.dy;
},
});
var styles = StyleSheet.create({
circle: {
width: CIRCLE_SIZE,
height: CIRCLE_SIZE,
borderRadius: CIRCLE_SIZE / 2,
position: 'absolute',
left: 0,
top: 0,
},
container: {
flex: 1,
paddingTop: 64,
},
});
module.exports = PanResponderExample;