【问题标题】:react native circle transform translate animation反应原生圆变换翻译动画
【发布时间】:2018-06-03 07:43:33
【问题描述】:

嗨,我希望animated.view 像圆圈一样移动。我想用鼻窦来解决这个问题,但它不起作用。有人知道怎么做吗?我不想旋转视图。它应该在圆圈中移动。我是新来的反应本地人。如果有人可以帮助我,那就太好了。

//import liraries
import React, { Component } from 'react';
import { View, Text, StyleSheet, Animated, Button, TouchableOpacity } from 'react-native';

// create a component
class MyClass extends Component {
  constructor() {
    super()
    this.animated = new Animated.Value(0);
  }

  animate() {    
    this.animated.setValue(0)
    Animated.timing(this.animated, {
      toValue: Math.PI *2,
      duration: 1000,
    }).start();
  }


  render() {
    const translateY = this.animated.interpolate({
      inputRange: [0, Math.PI *2],
      outputRange: [0, 200]
    });
    const translateX = translateY
    const transform = [{ translateY }, {translateX}];
    return (
      <View style={styles.container}>
        <Animated.View style={[{ transform }]}>
          <TouchableOpacity style={styles.btn}>
            <Text>hallo</Text>
          </TouchableOpacity>
        </Animated.View>
        <Button title="Test" onPress={() => { 
          this.animate() 
          }} />
      </View>
    );
  }
}

// define your styles
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#2c3e50',
  },
  btn: {
    backgroundColor: 'red',
    justifyContent: 'center',
    alignItems: 'center',
    width: 50,
  }
});

//make this component available to the app
export default MyClass;

【问题讨论】:

    标签: javascript reactjs react-native react-animated


    【解决方案1】:

    如果您正在寻找高性能的圆形旋转。 (没有颤抖/复杂的数学)

    在这里

    https://rn-animiations.web.app/component/circular-animation

    完整代码:

    import React, {Component} from 'react';
    import {View, Text, Animated, StyleSheet, Easing} from 'react-native';
    
    export default class Circle extends Component {
        constructor() {
            super();
            this.animated = new Animated.Value(0);
            var inputRange = [0, 1];
            var outputRange = ['0deg', '360deg'];
            this.rotate = this.animated.interpolate({inputRange, outputRange});
            outputRange = ['0deg', '-360deg'];
            this.rotateOpposit = this.animated.interpolate({inputRange, outputRange});
        }
    
        componentDidMount() {
            this.animate();
        }
    
        animate() {
          Animated.loop(
            Animated.timing(this.animated, {
                toValue: 1,
                duration: 4000,
                useNativeDriver: true,
                easing: Easing.linear,
            }),
          ).start();
        }
        render() {
            const transform = [{rotate: this.rotate}];
            const transform1 = [{rotate: this.rotateOpposit}];
            return (
              <View style={styles.container}>
                <Animated.View style={[styles.item, {transform}]}>
                  <Animated.View style={[styles.dot, {transform: transform1}]}>
                    <Text style={styles.text}>Test</Text>
                  </Animated.View>
                </Animated.View>
              </View>
            );
        }
     }
     const styles = StyleSheet.create({
        container: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
        },
        item: {
            position: 'absolute',
            width: 100,
            height: 200, // this is the diameter of circle
        },
        dot: {
            width: '100%',
            height: 20,
            backgroundColor: 'red',
            position: 'absolute',
            alignItems: 'center',
            justifyContent: 'center',
        },
        text: {
            color: '#fff',
        },
     });
    

    结果:

    【讨论】:

    • 太棒了!谢谢你
    【解决方案2】:

    你必须用Trigonometric Function计算translateXtranslateY

    translateX对应Math.sin()translateY对应Math.cos()

    虽然react-native animated.interpolate 不支持函数回调,但是你可以通过分成几个部分来模拟它(我在我的代码示例中选择了 50 个):

    完整代码:

    export class App extends Component {
        constructor() {
            super()
            this.animated = new Animated.Value(0);
    
            var range = 1, snapshot = 50, radius = 100;
            /// translateX
            var inputRange = [], outputRange = [];
            for (var i=0; i<=snapshot; ++i) {
                var value = i/snapshot;
                var move = Math.sin(value * Math.PI * 2) * radius;
                inputRange.push(value);
                outputRange.push(move);
            }
            this.translateX = this.animated.interpolate({ inputRange, outputRange });
    
            /// translateY
            var inputRange = [], outputRange = [];
            for (var i=0; i<=snapshot; ++i) {
                var value = i/snapshot;
                var move = -Math.cos(value * Math.PI * 2) * radius;
                inputRange.push(value);
                outputRange.push(move);
            }
            this.translateY = this.animated.interpolate({ inputRange, outputRange });
        }
    
          animate() {
            this.animated.setValue(0)
            Animated.timing(this.animated, {
              toValue: 1,
              duration: 1000,
            }).start();
          }
    
    
          render() {
            const transform = [{ translateY: this.translateY }, {translateX: this.translateX}];
            return (
              <View style={styles.container}>
                <Animated.View style={[{ transform }]}>
                  <TouchableOpacity style={styles.btn}>
                    <Text>hallo</Text>
                  </TouchableOpacity>
                </Animated.View>
                <Button title="Test" onPress={() => { 
                  this.animate() 
                  }} />
              </View>
            );
          }
        }
    
        // define your styles
        const styles = StyleSheet.create({
          container: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
            backgroundColor: '#2c3e50',
          },
          btn: {
            backgroundColor: 'red',
            justifyContent: 'center',
            alignItems: 'center',
            width: 50,
          }
        });
    

    结果:

    【讨论】:

    • 也许你有时间可以看看这个问题吗? stackoverflow.com/questions/47923061/…
    • @Val 如果我创建两个不同的数组 outputRangeX 和 outputRangeY 并且只在循环中推送值,它会停止工作吗?
    • @Val 如果我想绕一圈移动它怎么办?你能解释一下这是如何工作的吗?
    • @Paras 如何定义“移动它”?请提出一个新问题并将链接分享给我
    • @Val 我怎样才能让它从特定角度开始并停止在特定角度
    猜你喜欢
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2019-10-16
    • 2021-12-15
    相关资源
    最近更新 更多