2015-03-24 18:31:07 +00:00
|
|
|
/**
|
2016-04-05 17:50:42 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-03-28 05:18:47 +00:00
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
2015-03-26 07:41:30 +00:00
|
|
|
*
|
2015-03-28 05:18:47 +00:00
|
|
|
* Facebook reserves all rights not expressly granted.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2015-03-26 07:41:30 +00:00
|
|
|
*
|
2015-12-02 03:09:01 +00:00
|
|
|
* @flow weak
|
2015-03-24 18:31:07 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react-native');
|
|
|
|
var {
|
|
|
|
PanResponder,
|
2015-09-17 15:36:08 +00:00
|
|
|
StyleSheet,
|
2015-03-24 18:31:07 +00:00
|
|
|
View,
|
2015-09-17 15:36:08 +00:00
|
|
|
processColor,
|
2015-03-24 18:31:07 +00:00
|
|
|
} = React;
|
|
|
|
|
|
|
|
var CIRCLE_SIZE = 80;
|
|
|
|
|
2015-06-10 00:18:56 +00:00
|
|
|
var PanResponderExample = React.createClass({
|
2015-03-24 18:31:07 +00:00
|
|
|
|
|
|
|
statics: {
|
|
|
|
title: 'PanResponder Sample',
|
2015-09-15 21:46:54 +00:00
|
|
|
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: {},
|
2015-04-03 20:19:24 +00:00
|
|
|
circle: (null : ?{ setNativeProps(props: Object): void }),
|
2015-03-24 18:31:07 +00:00
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
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 = {
|
2015-10-10 00:29:45 +00:00
|
|
|
style: {
|
|
|
|
left: this._previousLeft,
|
2016-04-05 17:50:42 +00:00
|
|
|
top: this._previousTop,
|
|
|
|
backgroundColor: 'green',
|
2015-10-10 00:29:45 +00:00
|
|
|
}
|
2015-03-24 18:31:07 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2016-04-05 17:50:42 +00:00
|
|
|
this._updateNativeStyles();
|
2015-03-24 18:31:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={styles.container}>
|
|
|
|
<View
|
|
|
|
ref={(circle) => {
|
|
|
|
this.circle = circle;
|
|
|
|
}}
|
|
|
|
style={styles.circle}
|
|
|
|
{...this._panResponder.panHandlers}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_highlight: function() {
|
2016-04-05 17:50:42 +00:00
|
|
|
this._circleStyles.style.backgroundColor = 'blue';
|
|
|
|
this._updateNativeStyles();
|
2015-03-24 18:31:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_unHighlight: function() {
|
2016-04-05 17:50:42 +00:00
|
|
|
this._circleStyles.style.backgroundColor = 'green';
|
|
|
|
this._updateNativeStyles();
|
2015-03-24 18:31:07 +00:00
|
|
|
},
|
|
|
|
|
2016-04-05 17:50:42 +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 {
|
|
|
|
// Should we become active when the user presses down on the circle?
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleMoveShouldSetPanResponder: function(e: Object, gestureState: Object): boolean {
|
|
|
|
// 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) {
|
2015-10-10 00:29:45 +00:00
|
|
|
this._circleStyles.style.left = this._previousLeft + gestureState.dx;
|
|
|
|
this._circleStyles.style.top = this._previousTop + gestureState.dy;
|
2016-04-05 17:50:42 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-06-10 00:18:56 +00:00
|
|
|
module.exports = PanResponderExample;
|