如何在3D空间中的方向上绘制和旋转箭头?

时间:2015-07-27 00:22:38

标签: ios objective-c swift core-animation catransform3d

我想在某个方向上绘制如下图像。

enter image description here

假设设备平放在桌子上,上图显示了如何绘制箭头。设备和表之间的角度称为alpha,在这种情况下它为零。箭头指向预定义的位置点。如果您在桌面上旋转设备,箭头将指向目标方向,就像指南针一样。

我在2D空间中使用以下代码更新了图像位置:

// Create the image for the compass
let arrowImageView: UIImageView = UIImageView(frame: CGRectMake(100, 200, 100, 100))
arrowImageView.image = UIImage(named: "arrow.png")
self.view.addSubview(arrowImageView)
arrowImageView.center = self.view.center


func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {

    var direction: Float = Float(newHeading.magneticHeading)

    if direction > 180 {
      direction = 360 - direction
    }
    else {
      direction = 0 - direction
    }

    // Rotate the arrow image
    if let arrowImageView = self.arrowImageView {
      UIView.animateWithDuration(3.0, animations: { () -> Void in
        arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(self.degreesToRadians(direction) + self.angle))

      })
    }

    let currentLocation: CLLocation = manager.location
}

但是,如果设备从表中抬起,则alpha值会增加(alpha > 0)。箭头图像仍应指向目标位置,但应以透视方式绘制,同时考虑角度。这是从侧面看的样子。

enter image description here

3D空间中唯一发生变化的是alpha角度。所有其他角度保持不变。

如何在3D空间中以方向(给定的α角度)绘制和旋转箭头图像?

0 个答案:

没有答案