Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. View,
  5. Animated,
  6. ViewPropTypes,
  7. Easing,
  8. } from 'react-native';
  9. import { find } from '@Utilities/index';
  10.  
  11. export default class Animation extends PureComponent {
  12. static ANIMATION_KEY_REGEX = /.+?{animValue}.+?/;
  13.  
  14. constructor(props) {
  15. super(props);
  16. this.animatedValue = new Animated.Value(this.props.initialValue);
  17. }
  18.  
  19. componentWillUnmount() {
  20. this.animatedValue.removeAllListeners();
  21. }
  22.  
  23. shouldComponentUpdate(nextProps) {
  24. this.rotate(nextProps);
  25. return true;
  26. }
  27.  
  28. animate = (toValue) => {
  29. this.onStart();
  30. this.animation(toValue).start(this.onFinish);
  31. };
  32.  
  33. animation = toValue => Animated.timing(
  34. this.animatedValue,
  35. {
  36. toValue,
  37. delay: this.props.delay,
  38. easing: this.props.easing,
  39. duration: this.props.duration,
  40. isInteraction: this.props.isInteraction,
  41. useNativeDriver: this.props.useNativeDriver,
  42. },
  43. );
  44.  
  45. animatedStyle = () => ({
  46. ...this.props.style,
  47.  
  48. });
  49.  
  50. calculateAnimationStyle = () => {
  51.  
  52. }
  53.  
  54. getAnimationPaths = () => find(this.props.style, Animation.ANIMATION_KEY_REGEX);
  55.  
  56. onFinish = () => {
  57. this.props.onFinish();
  58. };
  59.  
  60. onStart = () => {
  61. this.props.onStart();
  62. };
  63.  
  64. render() {
  65. const animatedStyle = this.animatedStyle();
  66. return (
  67. <Animated.View style={[
  68. this.props.style,
  69. animatedStyle,
  70. ]}
  71. >
  72. {this.props.children}
  73. </Animated.View>
  74. );
  75. }
  76. }
  77.  
  78. Animation.defaultProps = {
  79. initialValue: 0,
  80. style: {},
  81. animationStyle: {},
  82. children: <View />,
  83. onFinish: () => { },
  84. onStart: () => { },
  85.  
  86. // animation default props
  87. delay: 0,
  88. duration: 1000,
  89. easing: Easing.out(Easing.ease),
  90. };
  91.  
  92. Animation.propTypes = {
  93. initialValue: PropTypes.number,
  94. style: ViewPropTypes.style,
  95. animationStyle: ViewPropTypes.style,
  96. onFinish: PropTypes.func,
  97. onStart: PropTypes.func,
  98. children: PropTypes.oneOfType([
  99. PropTypes.arrayOf(PropTypes.node),
  100. PropTypes.node,
  101. ]).isRequired,
  102.  
  103. // animation props
  104. duration: PropTypes.number,
  105. easing: PropTypes.func,
  106. delay: PropTypes.number,
  107. isInteraction: PropTypes.bool,
  108. useNativeDriver: PropTypes.bool,
  109. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement