Проблема с реагированием нативной анимации на Scroll с использованием реанимации, перерисовки - PullRequest
0 голосов
/ 05 марта 2020

Здравствуйте, я пытаюсь реализовать анимацию в React Native. Когда я прокручиваю вверх от scrollview, я хочу, чтобы один из дочерних элементов в scrollview, а именно buttonContainer, исчез. Итак, когда я начинаю прокручивать вверх, непрозрачность buttonContainer может измениться с 1 на 0. Но ничего не происходит. Я не совсем понимаю, что поставить входные и выходные диапазоны.

Вот код в Снэк

и здесь также

import * as React from 'react';
import { Text, View, StyleSheet, Dimensions, Button } from 'react-native';
import Animated from 'react-native-reanimated';
import { onScroll } from 'react-native-redash';

const { height } = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const { Value, interpolate, Extrapolate } = Animated;

export default class App extends React.Component {
  render() {
    const scrollY = new Value(0);
    const opacity = interpolate(scrollY, {
      inputRange: [BUTTON_CONTAINER_HEIGHT, height - 30],
      outputRange: [1, 0],
      extrapolate: Extrapolate.CLAMP,
    });
    return (
      <View style={{ flex: 1 }}>
        <View style={styles.container}>
          <Animated.ScrollView
            onScroll={onScroll({ scrollY })}
            showVerticalScrollIndicator={false}
            scrollEventThrottle={1}>
            <Animated.View
              style={[styles.buttonContainer, { opacity: opacity }]}>
              <Text>Hello World! </Text>
              <Button title="Click Me!" />
            </Animated.View>
            <View>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
            </View>
          </Animated.ScrollView>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    position: 'relative',
  },
  buttonContainer: {
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'flex-end',
    alignItems: 'center',
    height: BUTTON_CONTAINER_HEIGHT,
    marginBottom: 30,
  },
  textStyle: {
    height: 100,
    fontSize: 24,
    marginTop: 10,
  },
});



1 Ответ

2 голосов
/ 05 марта 2020

вы определяете анимированное значение неправильно, не определяйте значения в методе рендеринга, поскольку они будут создаваться каждый раз при рендеринге компонента, определяйте их в конструкторе ,,,, используйте приведенный ниже код

import * as React from 'react';
import {Text, View, StyleSheet, Dimensions, Button} from 'react-native';
import {onScroll} from 'react-native-redash';
import Animated from 'react-native-reanimated';

const {height} = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const {Value, interpolate, Extrapolate} = Animated;

export default class App extends React.Component<{}> {
  constructor(props: Readonly<{}>) {
    super(props);
    this.scrollY = new Value(0);
  }
  render() {
    const {scrollY} = this;
    const opacity = interpolate(scrollY, {
      inputRange: [0, BUTTON_CONTAINER_HEIGHT, height - 30],
      outputRange: [1, 0, 0],
      extrapolate: Extrapolate.CLAMP,
    });
    return (
      <View style={{flex: 1}}>
        <View style={styles.container}>
          <Animated.ScrollView
            onScroll={onScroll({y: scrollY})}
            showsVerticalScrollIndicator={false}
            scrollEventThrottle={1}>
            <Animated.View style={[styles.buttonContainer, {opacity}]}>
              <Text>Hello World! </Text>
              <Button onPress={() => {}} title="Click Me!" />
            </Animated.View>
            <View>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
              <Text style={styles.textStyle}>This is content</Text>
            </View>
          </Animated.ScrollView>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    position: 'relative',
  },
  buttonContainer: {
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'flex-end',
    alignItems: 'center',
    height: BUTTON_CONTAINER_HEIGHT,
    marginBottom: 30,
  },
  textStyle: {
    height: 100,
    fontSize: 24,
    marginTop: 10,
  },
});

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...