react native中使用Animated实现三张图片动态旋转效果

react native中使用Animated实现三张图片动态旋转效果

效果示例图


示例代码

复制代码
import React, {useEffect, useRef} from 'react';
import {Animated, StyleSheet, View} from 'react-native';
import {pxToPd} from '../../common/js/device';

const TestShowCard = () => {
  const rotationValue = useRef(new Animated.Value(0)).current;

  const firstCardRotation = '0deg';
  const secondCardRotation = rotationValue.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '-15deg'],
  });
  const thirdCardRotation = rotationValue.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '15deg'],
  });

  useEffect(() => {
    Animated.timing(rotationValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true,
    }).start();
    return () => {};
  }, []);

  return (
    <>
      <View style={styles.container}>
        <View style={styles.dynCard}>
          <Animated.View
            style={[
              styles.cardItem,
              {transform: [{rotate: thirdCardRotation}]},
            ]}></Animated.View>
          <Animated.View
            style={[
              styles.cardItem,
              {transform: [{rotate: secondCardRotation}]},
            ]}></Animated.View>
          <Animated.View
            style={[
              styles.cardItem,
              {transform: [{rotate: firstCardRotation}]},
            ]}></Animated.View>
        </View>
      </View>
    </>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  dynCard: {
    width: pxToPd(174),
    height: pxToPd(203),
    position: 'relative',
    marginTop: pxToPd(200),
    marginLeft: pxToPd(100),
  },
  cardItem: {
    width: '100%',
    height: '100%',
    borderColor: 'blue',
    borderWidth: pxToPd(1),
    borderStyle: 'solid',
    position: 'absolute',
    backgroundColor: 'white',
  },
});

export default TestShowCard;
相关推荐
张元清2 分钟前
React useResizeObserver Hook:追踪元素尺寸变化(2026)
javascript·react.js
瑞瑞小同学3 小时前
处理echarts x轴内容过多,重叠问题
前端·javascript·echarts
像我这样帅的人丶你还4 小时前
MCP + npm:给五年前的老系统接上AI
前端·javascript·agent
葬送的代码人生4 小时前
从 Vue 到 React:Tailwind CSS 布局 + BFF 代理实战
前端·react.js·架构
猫头_5 小时前
AI 流式传输工程指南:有了 EventSource 为何还要 Fetch?
javascript·http·llm
良木林6 小时前
滑动窗口 - LeetCode hot 100
javascript·算法·leetcode·双指针·滑动窗口
不简说8 小时前
# JS 代码技巧 vol.7 — 20 个浏览器 API 实战,自带 API 能干的事别自己封装
前端·javascript·面试
Hilaku8 小时前
为什么 Cloudflare 能做到毫秒级冷启动,而其它云厂商却不行?
前端·javascript·程序员
名字还没想好☜8 小时前
React useLayoutEffect vs useEffect:页面闪一下的元凶与正确修法
前端·javascript·react.js·react·hooks
2301_794461578 小时前
JavaScript 基础知识点详解
前端·javascript·html