Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. PanResponder,
  4. StyleSheet,
  5. View,
  6. Text
  7. } from 'react-native';
  8.  
  9. export default class T7Ej1 extends Component{
  10. constructor(props){
  11. super(props);
  12. this._panResponder = {};
  13. this._previousLeft = props.x;
  14. this._previousTop = props.y;
  15. this._circleStyles = {};
  16. this.circle = null;
  17.  
  18. this._panResponder = PanResponder.create({
  19. onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
  20. onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
  21. onPanResponderGrant: this._handlePanResponderGrant,
  22. onPanResponderMove: this._handlePanResponderMove,
  23. onPanResponderRelease: this._handlePanResponderEnd,
  24. onPanResponderTerminate: this._handlePanResponderEnd,
  25. });
  26. this._circleStyles = {
  27. style: {
  28. left: this._previousLeft,
  29. top: this._previousTop,
  30. backgroundColor: 'green',
  31. borderRadius: 80
  32. }
  33. };
  34. }
  35.  
  36. componentDidMount = () => {
  37. this._updateNativeStyles();
  38. }
  39.  
  40. _highlight = () => {
  41. this._circleStyles.style.backgroundColor = 'blue';
  42. this._updateNativeStyles();
  43. }
  44. _unHighlight = () => {
  45. this._circleStyles.style.backgroundColor = 'green';
  46. this._updateNativeStyles();
  47. }
  48. _updateNativeStyles = () => {
  49. this.circle && this.circle.setNativeProps(this._circleStyles);
  50. }
  51. _handleStartShouldSetPanResponder = (e, gestureState) => {
  52. return true;
  53. }
  54. _handleMoveShouldSetPanResponder = (e, gestureState) => {
  55. return true;
  56. }
  57. _handlePanResponderGrant = (e, gestureState) => {
  58. this._highlight();
  59. }
  60. _handlePanResponderMove = (e, gestureState) => {
  61. this._circleStyles.style.left = this._previousLeft + gestureState.dx;
  62. this._circleStyles.style.top = this._previousTop + gestureState.dy;
  63. this._updateNativeStyles();
  64. }
  65. _handlePanResponderEnd = (e, gestureState) => {
  66. this._unHighlight();
  67. this._previousLeft += gestureState.dx;
  68. this._previousTop += gestureState.dy;
  69. }
  70.  
  71. render() {
  72. return (
  73. <View
  74. ref={(circle) => {
  75. this.circle = circle;
  76. }}
  77. style={styles.circle}
  78. {...this._panResponder.panHandlers}
  79. />
  80. );
  81. }
  82. }
  83.  
  84. const styles = StyleSheet.create({
  85. circle: {
  86. width: 80,
  87. height: 80,
  88. borderRadius: 80,
  89. position: 'absolute',
  90. left: 0,
  91. top: 0,
  92. },
  93. container: {
  94. flex: 1,
  95. paddingTop: 0,
  96. },
  97. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement