InteractableView.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react'
  2. import ReactNative, {
  3. requireNativeComponent,
  4. Animated,
  5. NativeModules,
  6. UIManager,
  7. Platform,
  8. } from 'react-native'
  9. // this is required in order to perform imperative commands
  10. const NativeViewManager = NativeModules.InteractableViewManager
  11. const NativeInteractableView = requireNativeComponent('InteractableView', null)
  12. class WrappedInteractableView extends Component {
  13. render() {
  14. return (
  15. <NativeInteractableView
  16. {...this.props}
  17. ref={ref => (this._nativeViewRef = ref)}
  18. />
  19. )
  20. }
  21. getScrollableNode() {
  22. return ReactNative.findNodeHandle(this._nativeViewRef)
  23. }
  24. }
  25. // this is required in order to support native events:
  26. const AnimatedInteractableView = Animated.createAnimatedComponent(
  27. WrappedInteractableView
  28. )
  29. class WrappedAnimatedInteractableView extends Component {
  30. constructor(props) {
  31. super(props)
  32. if (this.props.animatedValueX || this.props.animatedValueY) {
  33. this._animatedEvent = Animated.event(
  34. [
  35. {
  36. nativeEvent: {
  37. x: this.props.animatedValueX,
  38. y: this.props.animatedValueY,
  39. },
  40. },
  41. ],
  42. { useNativeDriver: !!this.props.animatedNativeDriver }
  43. )
  44. }
  45. }
  46. componentWillMount() {
  47. // this.chokeTheBridge();
  48. }
  49. // this helps us verify that useNativeDriver actually works and we don't rely on the bridge
  50. chokeTheBridge() {
  51. let j = 0
  52. setInterval(() => {
  53. for (var index = 0; index < 1e9; index++) {
  54. j++
  55. }
  56. }, 500)
  57. }
  58. render() {
  59. return (
  60. <AnimatedInteractableView
  61. dragToss={0.1}
  62. {...this.props}
  63. animatedValueX={undefined}
  64. animatedValueY={undefined}
  65. onAnimatedEvent={this._animatedEvent}
  66. reportOnAnimatedEvents={!!this._animatedEvent}
  67. />
  68. )
  69. }
  70. setVelocity(params) {
  71. if (Platform.OS === 'ios') {
  72. NativeViewManager.setVelocity(ReactNative.findNodeHandle(this), params)
  73. } else if (Platform.OS === 'android') {
  74. UIManager.dispatchViewManagerCommand(
  75. ReactNative.findNodeHandle(this),
  76. UIManager.InteractableView.Commands.setVelocity,
  77. [params]
  78. )
  79. }
  80. }
  81. snapTo(params) {
  82. if (Platform.OS === 'ios') {
  83. NativeViewManager.snapTo(ReactNative.findNodeHandle(this), params)
  84. } else if (Platform.OS === 'android') {
  85. UIManager.dispatchViewManagerCommand(
  86. ReactNative.findNodeHandle(this),
  87. UIManager.InteractableView.Commands.snapTo,
  88. [params]
  89. )
  90. }
  91. }
  92. changePosition(params) {
  93. if (Platform.OS === 'ios') {
  94. NativeViewManager.changePosition(ReactNative.findNodeHandle(this), params)
  95. } else if (Platform.OS === 'android') {
  96. UIManager.dispatchViewManagerCommand(
  97. ReactNative.findNodeHandle(this),
  98. UIManager.InteractableView.Commands.changePosition,
  99. [params]
  100. )
  101. }
  102. }
  103. bringToFront() {
  104. if (Platform.OS === 'android') {
  105. UIManager.dispatchViewManagerCommand(
  106. ReactNative.findNodeHandle(this),
  107. UIManager.InteractableView.Commands.bringToFront,
  108. []
  109. )
  110. }
  111. }
  112. }
  113. export default WrappedAnimatedInteractableView