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>
}
相关推荐
Dragon Wu20 小时前
React Native cli 集成Op-Sqlite编译超时问题解决
react native
光影少年1 天前
react离线缓存、图片缓存方案
开发语言·前端·javascript·react native·react.js·缓存·前端框架
光影少年3 天前
react navite本地存储:AsyncStorage、MMKV、文件存储
前端·react native·react.js
梦想的颜色3 天前
AI 时代小白 VibeCoding 做 APP:UniApp(含 Uni‑X)、Flutter 与 React Native+Expo全维度技术选型对比指南
flutter·react native·app·uniapp·vibecoding·unippx·app产品
AI砖家3 天前
React Native 开发规范与完整流程指南
javascript·react native·react.js
光影少年4 天前
RN 网络请求:Fetch / Axios 封装、超时、拦截器
前端·react native·react.js
墨狂之逸才5 天前
React Native一次人脸注册引发的 MMKV 数据污染排查复盘
react native
光影少年5 天前
React Native网络、存储与原生能力
前端·react native·react.js