如何在圆上移动视图

时间:2018-10-02 16:09:59

标签: javascript react-native

通过此代码,我可以在屏幕上围成一圈。

  render() {

    var obj = {}
    var _elements = [obj,obj,obj,obj]
    var elemens = []
    var width = Dimensions.get('window').width
    var height = Dimensions.get('window').height

    var radius = 100;
    var angle = 0
    var step = (2*Math.PI) / _elements.length;

    _elements.map(element => {
        var x = Math.round(width/2 + radius * Math.cos(angle) - 40/2);
        var y = Math.round(height/2 + radius * Math.sin(angle) - 40/2);

        var element = {}
        element.position    = 'absolute'
        element.top         = y
        element.left        = x
        element.width       = 40
        element.height      = 40
        element.backgroundColor = '#000'

        angle += step;

        elemens.push(element);
    });


    return (
      <View style={{flex:1,flexDirection:'column'}}>

      {elemens.map(element => {
        return(<View style={{position:'absolute',top:element.top,left:element.left, width:10,height:10,backgroundColor:'#000',color:'#000'}}/>)
      })}


      </View>
    );
  }

结果:

enter image description here

现在我要用户可以移动其中之一,而其他视图沿圆线移动(保持距离)。

0 个答案:

没有答案