mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
7abf8ba631
Summary: Fixes #3312.
The example was no longer reacting to touch/drag events.
It looks like `setNativeProps` requires the `style` property to contain the style rules. It appears that this warning is [only in development mode](381e2acd18/Libraries/ReactIOS/NativeMethodsMixin.js (L115-L117)
), but after fixing it I am seeing the gestures recognized and properly handled. Running outside of the dev environment yielded the circle to be positioned at the top left of the screen, below the navigation bar, and inaccessible.
<img src="https://cloud.githubusercontent.com/assets/656630/10407224/1449b1b2-6eb6-11e5-8116-20d08f7721ec.png" width="350" />
![after](http://g.recordit.co/UIbeHcKZUg.gif)
Closes https://github.com/facebook/react-native/pull/3315
Reviewed By: @svcscm
Differential Revision: D2529010
Pulled By: @vjeux
fb-gh-sync-id: 6b79573f42a2520197f77dcb76cd640ea614477f
143 lines
3.8 KiB
JavaScript
143 lines
3.8 KiB
JavaScript
/**
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
* evaluation purposes only.
|
|
*
|
|
* 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.
|
|
*
|
|
* @flow-weak
|
|
*/
|
|
'use strict';
|
|
|
|
var React = require('react-native');
|
|
var {
|
|
PanResponder,
|
|
StyleSheet,
|
|
View,
|
|
processColor,
|
|
} = React;
|
|
|
|
var CIRCLE_SIZE = 80;
|
|
var CIRCLE_COLOR = 'blue';
|
|
var CIRCLE_HIGHLIGHT_COLOR = 'green';
|
|
|
|
var PanResponderExample = React.createClass({
|
|
|
|
statics: {
|
|
title: 'PanResponder Sample',
|
|
description: 'Shows the use of PanResponder to provide basic gesture handling.',
|
|
},
|
|
|
|
_panResponder: {},
|
|
_previousLeft: 0,
|
|
_previousTop: 0,
|
|
_circleStyles: {},
|
|
circle: (null : ?{ setNativeProps(props: Object): void }),
|
|
|
|
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 = {
|
|
style: {
|
|
left: this._previousLeft,
|
|
top: this._previousTop
|
|
}
|
|
};
|
|
},
|
|
|
|
componentDidMount: function() {
|
|
this._updatePosition();
|
|
},
|
|
|
|
render: function() {
|
|
return (
|
|
<View
|
|
style={styles.container}>
|
|
<View
|
|
ref={(circle) => {
|
|
this.circle = circle;
|
|
}}
|
|
style={styles.circle}
|
|
{...this._panResponder.panHandlers}
|
|
/>
|
|
</View>
|
|
);
|
|
},
|
|
|
|
_highlight: function() {
|
|
this.circle && this.circle.setNativeProps({
|
|
style: {
|
|
backgroundColor: processColor(CIRCLE_HIGHLIGHT_COLOR)
|
|
}
|
|
});
|
|
},
|
|
|
|
_unHighlight: function() {
|
|
this.circle && this.circle.setNativeProps({
|
|
style: {
|
|
backgroundColor: processColor(CIRCLE_COLOR)
|
|
}
|
|
});
|
|
},
|
|
|
|
_updatePosition: function() {
|
|
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) {
|
|
this._circleStyles.style.left = this._previousLeft + gestureState.dx;
|
|
this._circleStyles.style.top = this._previousTop + gestureState.dy;
|
|
this._updatePosition();
|
|
},
|
|
_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,
|
|
backgroundColor: CIRCLE_COLOR,
|
|
position: 'absolute',
|
|
left: 0,
|
|
top: 0,
|
|
},
|
|
container: {
|
|
flex: 1,
|
|
paddingTop: 64,
|
|
},
|
|
});
|
|
|
|
module.exports = PanResponderExample;
|