How can I draw and rotate an Arrow at an Orientati

2019-05-29 19:24发布

I want to draw an image like the following in a certain orientation.

enter image description here

Assume that the device lies flat on a table the image above shows how the arrow should be drawn. The angle between the device and the table is called alpha and it is zero in this case. The arrow points straight to a predefined location point. If you rotate the device on the table the arrow will point alway to the target direction just like a compass.

I did the update of the image position with the following code in 2D space:

// 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
}

However if the device is lifted up from the table the value of alpha increases (alpha > 0). The arrow image should still point to the target location but should be drawn in perspective keeping the alpha angle into account. Here is how this looks from the side.

enter image description here

The only thing which changes in 3D space is the alpha angle. All other angles remain constant.

How can I draw and rotate an Arrow image at an Orientation (given alpha angle) in 3D Space?

0条回答
登录 后发表回答