Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在react native中基于滚动设置要显示的标题动画?_React Native_React Animated - Fatal编程技术网

React native 如何在react native中基于滚动设置要显示的标题动画?

React native 如何在react native中基于滚动设置要显示的标题动画?,react-native,react-animated,React Native,React Animated,所以理想情况下,当我向下滚动时,我希望标题消失,向下滑动,当我向上滚动时,我希望它显示向上滑动。Idc,我在页面中的位置。我只想在这两个事件发生时启动动画。我看到一些应用程序有这个功能,但我想不出如何复制它。请帮助我为此设置基础。您可以使用Animated.FlatList或Animated.ScrollView创建滚动视图,并在更改时附加回调以侦听onScroll事件。然后,使用插值在y轴和不透明度之间映射值 searchBarOpacityAnim是组件的状态。通过使用Animated.ev

所以理想情况下,当我向下滚动时,我希望标题消失,向下滑动,当我向上滚动时,我希望它显示向上滑动。Idc,我在页面中的位置。我只想在这两个事件发生时启动动画。我看到一些应用程序有这个功能,但我想不出如何复制它。请帮助我为此设置基础。您可以使用Animated.FlatList或Animated.ScrollView创建滚动视图,并在更改时附加回调以侦听onScroll事件。然后,使用插值在y轴和不透明度之间映射值

searchBarOpacityAnim是组件的状态。通过使用Animated.event,调用回调时将更新状态。另外,不要忘记将useNativeDriver设置为true。我已经附上了下面文档的链接,说明了为什么要设置它

<Animated.FlatList
  ...
  onScroll={Animated.event(
    [{ nativeEvent: { contentOffset: { y: searchBarOpacityAnim } } }],
    { useNativeDriver: true },
  )}
  ...
/>
您可以在此处阅读有关useNativeDriver、.interpolate和Animated.event的更多信息

您可以使用Animated.FlatList或Animated.ScrollView创建滚动视图,并在更改onScroll事件时附加回调以侦听该事件。然后,使用插值在y轴和不透明度之间映射值

searchBarOpacityAnim是组件的状态。通过使用Animated.event,调用回调时将更新状态。另外,不要忘记将useNativeDriver设置为true。我已经附上了下面文档的链接,说明了为什么要设置它

<Animated.FlatList
  ...
  onScroll={Animated.event(
    [{ nativeEvent: { contentOffset: { y: searchBarOpacityAnim } } }],
    { useNativeDriver: true },
  )}
  ...
/>
您可以在此处阅读有关useNativeDriver、.interpolate和Animated.event的更多信息


您可以使用“react native”中的动画

下面是更改顶栏高度的示例:

import { Animated } from 'react-native';
定义最大高度和最小高度顶部栏

const HEADER_MAX_HEIGHT = 120;
const HEADER_MIN_HEIGHT = 48;
使用滚动值初始化变量

constructor(props) {
   super(props);

   this.state = {
       scrollY: new Animated.Value(
          Platform.OS === 'ios' ? -HEADER_MAX_HEIGHT : 0,
       ),
   };
}
在渲染时,可以根据滚动值插值

render() {
   const { scrollY } = this.state;

   // this will set a height for topbar
   const headerHeight = scrollY.interpolate({
      inputRange: [0, HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT],
      outputRange: [HEADER_MAX_HEIGHT, HEADER_MIN_HEIGHT],
      extrapolate: 'clamp',
   });

   // obs: the inputRange is the scrollY value, (starts on 0) 
   //  and can go until (HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT)
   //  outputRange is the height that will set on topbar

   // obs: you must add a onScroll function on a scrollView like below:

   return (
       <View>
          <Animated.View style={{
             position: 'absolute',
             top: 0,
             left: 0,
             right: 0,
             backgroundColor: '#2e4265',
             height: headerHeight,
             zIndex: 1,
             flexDirection: 'row',
             justifyContent: 'flex-start',
           }}>
             <Text>{title}</Text>
         </Animated.View>
         <ScrollView
            style={{ flex: 1 }}
            scrollEventThrottle={16}
            onScroll={Animated.event(
               [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
          )}>
            <View style={{ height: 1000 }} />
         </ScrollView>
       </View>
   );
}

您可以使用“react native”中的动画

下面是更改顶栏高度的示例:

import { Animated } from 'react-native';
定义最大高度和最小高度顶部栏

const HEADER_MAX_HEIGHT = 120;
const HEADER_MIN_HEIGHT = 48;
使用滚动值初始化变量

constructor(props) {
   super(props);

   this.state = {
       scrollY: new Animated.Value(
          Platform.OS === 'ios' ? -HEADER_MAX_HEIGHT : 0,
       ),
   };
}
在渲染时,可以根据滚动值插值

render() {
   const { scrollY } = this.state;

   // this will set a height for topbar
   const headerHeight = scrollY.interpolate({
      inputRange: [0, HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT],
      outputRange: [HEADER_MAX_HEIGHT, HEADER_MIN_HEIGHT],
      extrapolate: 'clamp',
   });

   // obs: the inputRange is the scrollY value, (starts on 0) 
   //  and can go until (HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT)
   //  outputRange is the height that will set on topbar

   // obs: you must add a onScroll function on a scrollView like below:

   return (
       <View>
          <Animated.View style={{
             position: 'absolute',
             top: 0,
             left: 0,
             right: 0,
             backgroundColor: '#2e4265',
             height: headerHeight,
             zIndex: 1,
             flexDirection: 'row',
             justifyContent: 'flex-start',
           }}>
             <Text>{title}</Text>
         </Animated.View>
         <ScrollView
            style={{ flex: 1 }}
            scrollEventThrottle={16}
            onScroll={Animated.event(
               [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
          )}>
            <View style={{ height: 1000 }} />
         </ScrollView>
       </View>
   );
}