react native中利用插值实现自定义动画效果

设想在RN如何实现一段抽奖转盘的动画:20%前慢,20-80%快,80-90%减慢,90-100%摇摆一下回正。 难道用顺序动画去写?多种实现中感觉插值更方便,也许你没注意到它在动画中的使用。

官方代码:

js 复制代码
value.interpolate({ 
        inputRange: [-300, -100, 0, 100, 101], 
        outputRange: [300, 0, 1, 0, 0] 
    });

我们可以创建一个线性动画0-1,作为inputRange。然后outputRange输出"0deg"到终点角度,对应关系可以这么做:

js 复制代码
inputRange: [0, 0.2, 0.8, 0.9, 1], 
outputRange: ['0deg', '100deg', '800deg', '830deg', '810deg']

输出是非线性的,以此为例可以做出你想要的动画效果。

实现代码:

js 复制代码
let [zAnim,setzAnim] = useState(new Animated.Value(0));
  let [dis,setDis]=useState(false)
  let getDeg = () => {
    return zAnim.interpolate({
      inputRange: [0, 0.2, 0.8, 0.9, 1],
      outputRange: ['0deg', '100deg', '800deg', '830deg', '810deg'],
      useNativeDriver: true
    });
  }
  let degAnim = getDeg()
  let ads = () => {
    Animated.timing(zAnim, {
      toValue: 1,
      duration: 4000
    }).start();
    setDis(true)
    setTimeout(() => {
      setzAnim(new Animated.Value(0))
      setDis(false)
    }, 4000)
  }
  return <View>
    <Animated.Image
      style={[styles.img, {
        transform: [
          { rotate: degAnim}
        ]
      }]}
      source={require("./img/logo.jpg")}
    />
    <Button title="开始" onPress={ads.bind(null)} disabled={dis}/>
  </View>
}
相关推荐
zh_xuan18 小时前
启动RN服务端口被占用
android·react native
墨狂之逸才1 天前
在 React Native 中集成 MinIO 对象存储(图片/文件上传服务)
react native
chenbin___1 天前
鸿蒙RN position: ‘absolute‘ 和 zIndex 的兼容性问题(转自千问)
前端·javascript·react native·harmonyos
未名编程1 天前
React Native WebView 加载远程页面显示错误内容的深层原因及解决方案
javascript·react native·react.js
chenbin___2 天前
检查hooks依赖的工具(转自千问)
开发语言·前端·javascript·react native·react.js
chenbin___2 天前
鸿蒙(HarmonyOS)支持 useNativeDriver的详细说明(转自千问)
前端·javascript·react native·react.js·harmonyos
黑臂麒麟2 天前
React Hooks 闭包陷阱:状态“丢失“的经典坑
javascript·react native·react.js·ecmascript
fix一个write十个3 天前
NativeWind v4 与 React Native UI Kit或三方库样式隔离指南
前端·react native
2601_949593653 天前
小白入门ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-fast-image
react native·react.js·harmonyos
sealaugh323 天前
react native(学习笔记第二课) 英语打卡微应用(1)-开始构建
笔记·学习·react native