【问题标题】:React Native how to rotate and translate at the same timeReact Native 如何同时旋转和平移
【发布时间】:2021-06-23 07:56:42
【问题描述】:

如何同时旋转和平移? 在下面的示例中,我期望正方形向右移动并围绕自己的轴旋转,保持相同的中心 y 轴。如果你能想象一个轮子在路上行驶

相反,它疯狂地飞走了

任何帮助将不胜感激

import React, { Component } from "react";
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Animated
} from "react-native";

class App extends Component {
  
  componentWillMount () {
    this._animatedValue = new Animated.Value(0);
  }
  componentDidMount () {
    Animated.timing(this._animatedValue, {
        toValue: 100,
        duration: 3000
    }).start(); 
  }
  
  render () {
    
    const interpolatedRotateAnimation = this._animatedValue.interpolate({
        inputRange: [0, 100],
      outputRange: ['0deg', '360deg']
    });

     const interpolatedRotateX = this._animatedValue.interpolate({
        inputRange: [0, 100],
      outputRange: [0, 200]
    });
    
    return (
      <View style={styles.container}>
       <Animated.View 
            style={[styles.box, {transform: [
            {rotate: interpolatedRotateAnimation},
                        {translateX:interpolatedRotateX}

            ]}
            ]}
        />
      </View>
    );
  }
};

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  box: {
    backgroundColor: 'red',
    position: 'absolute',
    top: 100,
    left: 100,
    width: 100,
    height: 100
  }
});

export default App

【问题讨论】:

    标签: react-native react-animated react-native-animatable


    【解决方案1】:

    我缺少的是轴是对象的中心。 平移是相对于由于旋转而发生变化的自身对象的轴。

    平移 x/y 不仅仅是相对于屏幕移动对象,而是相对于它自己的轴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多