首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ReactNative:根据backgroundColor滚动位置动画backgroundColor

ReactNative:根据backgroundColor滚动位置动画backgroundColor
EN

Stack Overflow用户
提问于 2021-12-15 01:16:38
回答 1查看 537关注 0票数 1

我有一个ScrollView,我想要一个背景颜色为白色的视图,如果滚动位置超过0,则将其更改为透明,如果滚动返回到0,则返回为白色。

我试着开始,但反应本地动画似乎疯狂,复杂,从vue.js背景回来。

这是我的代码:

代码语言:javascript
运行
复制
const [animation, setAnimation] = useState(new Animated.Value(0))

const handleScroll = (event) => {
        console.log(event.nativeEvent.contentOffset.y);
        var scroll = event.nativeEvent.contentOffset.y;
        if(scroll > 0)
        {
            handleAnimation();
        }
      };

      const boxInterpolation =  animation.interpolate({
        inputRange: [0, 1],
        outputRange:["rgb(255,255,255)" , "rgba(255,255,255,0)"]
      })
    const animatedStyle = {
        backgroundColor: boxInterpolation
      }


      const handleAnimation = () => {
        Animated.timing(animation, {
          toValue:1,
          duration: 1000,
          useNativeDriver: true
        }).start(); 
      };

以及我的观点

代码语言:javascript
运行
复制
<ScrollView showsVerticalScrollIndicator={false} scrollEventThrottle={16} onScroll={handleScroll} style={{flex:1,backgroundColor:'white',position:'relative'}}>
            <View style={{ width:'100%', height:505,position:'relative' ,backgroundColor:'red}}>
            </View>
            
            </ScrollView>
            <Animated.View style={{width:'100%',height:100,...animatedStyle, flexDirection:'row',justifyContent:'space-around',alignItems:'center',position:'absolute',bottom:0,left:0}}>
            </Animated.View>
EN

回答 1

Stack Overflow用户

发布于 2021-12-15 08:41:29

获得ScrollView x和y位置的推荐方法如下

代码语言:javascript
运行
复制
const scrolling = React.useRef(new Animated.Value(0)).current;

<Animated.ScrollView 
     onScroll={Animated.event(
      [{
        nativeEvent: {
          contentOffset: {
            y: scrolling,
          },
        },
      }],
      { useNativeDriver: false },
    )}
</Animated.View>

现在一切正常,这里有一个完整的例子(https://snack.expo.dev/@heytony01/ca5000),必须运行电话,而不是网络。下面是密码。

代码语言:javascript
运行
复制
import  React from 'react';
import { Button,View,Animated,ScrollView } from 'react-native';

export default function App() {

  const scrolling = React.useRef(new Animated.Value(0)).current;
  const boxInterpolation =  scrolling.interpolate({
    inputRange: [0, 100],
    outputRange:["rgb(255,255,255)" , "rgba(255,255,255,0)"],
  })
  const animatedStyle = {
    backgroundColor: boxInterpolation
  }

  return (
    <View style={{flex:1}}>
     <Animated.ScrollView showsVerticalScrollIndicator={false} scrollEventThrottle={16}  style={{flex:1,backgroundColor:'black',position:'relative'}}
     onScroll={Animated.event(
      [{
        nativeEvent: {
          contentOffset: {
            // 
            y: scrolling,
          },
        },
      }],
      { useNativeDriver: false },
    )}
     >
            <View style={{ width:'100%', height:505,position:'relative', backgroundColor:'red'}}>
            </View>
            
      </Animated.ScrollView>
      <Animated.View style={{width:'100%',height:100,...animatedStyle, flexDirection:'row',justifyContent:'space-around',alignItems:'center',position:'absolute',bottom:0,left:0,zIndex:1}}>
      
      </Animated.View>

    </View>
  );
}

此外,如果要在调试时打印动画值,则可以这样做。

代码语言:javascript
运行
复制
React.useEffect(()=>{

  scrolling.addListener(dic=>console.log(dic.value))

  return ()=> scrolling.removeAllListeners()
},[])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70357433

复制
相关文章

相似问题

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