首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用reanimated,redash的react原生动画onScroll的问题

使用reanimated,redash的react原生动画onScroll的问题
EN

Stack Overflow用户
提问于 2020-03-05 07:36:25
回答 1查看 2.1K关注 0票数 1

你好,我正在尝试在React Native中实现动画。当我从scrollview向上滚动时,我想要scrollview中的一个子项是buttonContainer淡出。因此,当我开始向上滚动时,buttonContainer的不透明度可能从1变为0。但是,什么都没有发生。我不完全理解如何设置输入范围和输出范围。

Here is code in Snack

这里也有

代码语言:javascript
复制
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,
  },
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-05 12:43:12

您正在以错误的方式定义动画值,不要在render方法中定义值,因为它们将在每次组件重新呈现时创建,请使用以下代码在构造函数、中定义它们

代码语言:javascript
复制
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,
  },
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60536247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档