我如何在3D空间的方向上绘制和旋转箭头?
我想在某个方向上绘制如下的图像。
假设设备平放在桌面上,上面的图像显示了如何绘制箭头。 设备和表格之间的angular度称为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 > 0
)。 箭头图像仍然应该指向目标位置,但应该保持alphaangular度的透视图。 这是从侧面看的。
3D空间中唯一改变的就是alpha
angular。 所有其他angular度保持不变。
如何绘制和旋转3D空间中的方向(给定阿尔法angular度)的箭头图像?