2.5 Events

1.Basic events

1.1.<TouchableHighlight/>

  1. class Touch extends Component {
  2. handlePress(){
  3. console.log('press');
  4. }
  5. handleLongPress(){
  6. console.log('longPress');
  7. }
  8. render() {
  9. return (
  10. <TouchableHighlight
  11. onPress={this.handlePress}
  12. onLongPress={this.handleLongPress}>
  13. <View>
  14. <Text>Press me!</Text>
  15. </View>
  16. </TouchableHighlight>
  17. );
  18. }
  19. }

1.2. <TextInput/>

  1. class Test extends Component {
  2. //...
  3. //handle events
  4. //...
  5. render() {
  6. return (
  7. <TextInput
  8. onBlur={...}
  9. onChange={...}
  10. onEndEditing={...}
  11. onSelectionChange={...}
  12. onSubmitEditing={...}
  13. </TextInput>
  14. );
  15. }
  16. }

1.3.DeviceEventEmitter

  1. //keyboardWillShow, keyboardDidShow, keyboardWillHide, keyboardDidHide
  2. //keyboardWillChangeFrame, keyboardDidChangeFrame
  3. //add the listener
  4. var listener = DeviceEventEmitter.addListener('keyboardWillShow', (e) =>{
  5. console.log('Event is fired!');
  6. });
  7. //remove the listener
  8. listener.remove();

2.Gesture Responder System

2.1 Lifecycle 2.5 Events  - 图1 2.2 example

  1. class Test extends Component {
  2. /* Capture handles */
  3. //the responder system bubbles up from the deepest component,
  4. //a parent View wants to prevent the child from becoming responder on a touch start
  5. handleStartShouldSetResponderCapture(evt){
  6. return true;
  7. }
  8. //the responder system bubbles up from the deepest component,
  9. //a parent View wants to prevent the child from becoming responder on a touch move
  10. handleMoveShouldSetResponderCapture(evt){
  11. return true;
  12. }
  13. /* Lifecycle handles */
  14. //Does this view want to become responder on the start of a touch?
  15. handleStartShouldSetResponder(evt){
  16. return true;
  17. }
  18. //Called for every touch move on the View when it is not the responder:
  19. //does this view want to "claim" touch responsiveness?
  20. handleMoveShouldSetResponder(evt){
  21. return true;
  22. }
  23. //The View is now responding for touch events.
  24. handleResponderGrant(evt){
  25. console.log('you are touching me');
  26. }
  27. //Something else is the responder right now and will not release it
  28. handleResponderReject(evt){
  29. console.log('please wait in line');
  30. }
  31. /* event handles */
  32. //touch move
  33. handleResponderMove(evt){
  34. console.log('touch move at:', 'X='+evt.pageX, 'Y='+evt.pageY);
  35. }
  36. //touch end/up
  37. handleResponderRelease(evt){
  38. console.log('touch end');
  39. }
  40. //Something else wants to become responder. Should this view release the responder?
  41. handleResponderTerminationRequest(evt){
  42. return true;
  43. }
  44. //touch cancel
  45. handleResponderTerminate(evt){
  46. console.log('touch canceled');
  47. }
  48. render() {
  49. return (
  50. <View
  51. onStartShouldSetResponderCapture={this.handleStartShouldSetResponderCapture}
  52. onMoveShouldSetResponderCapture={this.handleMoveShouldSetResponderCapture}
  53. onStartShouldSetResponder={this.handleStartShouldSetResponder}
  54. onMoveShouldSetResponder={this.handleMoveShouldSetResponder}
  55. onResponderGrant={this.handleResponderGrant}
  56. onResponderReject={this.handleResponderReject}
  57. onResponderMove={this.handleResponderMove}
  58. onResponderRelease={this.handleResponderRelease}
  59. onResponderTerminationRequest={this.handleResponderTerminationRequest}
  60. onResponderTerminate={this.handleResponderTerminate}>
  61. <Text>Press me!</Text>
  62. </View>
  63. );
  64. }
  65. }

2.3 evt is a synthetic touch event with the following form nativeEvent:

  • changedTouches - Array of all touch events that have changed since the last event
  • identifier - The ID of the touch
  • locationX - The X position of the touch, relative to the element
  • locationY - The Y position of the touch, relative to the element
  • pageX - The X position of the touch, relative to the root element
  • pageY - The Y position of the touch, relative to the root element
  • target - The node id of the element receiving the touch event
  • timestamp - A time identifier for the touch, useful for velocity calculation
  • touches - Array of all current touches on the screen
    3.PanResponder

3.1

  1. this._panResponder = PanResponder.create({
  2. // Ask to be the responder:
  3. onStartShouldSetPanResponder: (evt, gestureState) => true,
  4. onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
  5. onMoveShouldSetPanResponder: (evt, gestureState) => true,
  6. onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
  7. //touch start
  8. onPanResponderGrant: (evt, gestureState) => {},
  9. //touch move
  10. onPanResponderMove: (evt, gestureState) => {},
  11. onPanResponderTerminationRequest: (evt, gestureState) => true,
  12. //touch end/up
  13. onPanResponderRelease: (evt, gestureState) => {},
  14. //touch cancel
  15. onPanResponderTerminate: (evt, gestureState) => {},
  16. onShouldBlockNativeResponder: (evt, gestureState) => true,
  17. });

3.2 A gestureState object has the following:

  • stateID - ID of the gestureState- persisted as long as there at least one touch on screen
  • moveX - the latest screen coordinates of the recently-moved touch
  • moveY - the latest screen coordinates of the recently-moved touch
  • x0 - the screen coordinates of the responder grant
  • y0 - the screen coordinates of the responder grant
  • dx - accumulated distance of the gesture since the touch started
  • dy - accumulated distance of the gesture since the touch started
  • vx - current velocity of the gesture
  • vy - current velocity of the gesture
  • numberActiveTouches - Number of touches currently on screen
    3.3 PanResponder example in UIExplorer

原文: https://unbug.gitbooks.io/react-native-training/content/24_events.html