工作代碼斯威夫特4(同時控制左右和上下來,無論手機點是,其視圖將被更新)
import GoogleMaps
import CoreMotion
class ViewController: UIViewController, GMSPanoramaViewDelegate {
let motionManager = CMMotionManager()
override func loadView() {
let panoView = GMSPanoramaView(frame: .zero)
panoView.delegate = self
self.view = panoView
// you can choose any latitude longitude here, this is my random choice
panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: 48.858, longitude: 2.284))
motionManager.startDeviceMotionUpdates()
if motionManager.isGyroAvailable {
motionManager.startGyroUpdates(to: OperationQueue.main, withHandler: { (gyroData: CMGyroData?, error: Error?) in
// needed to figure out the rotation
let y = (gyroData?.rotationRate.y)!
let motion = self.motionManager.deviceMotion
if(motion?.attitude.pitch != nil) {
// calculate the pitch movement (up/down) I subtract 40 just as
// an offset to the view so it's more at face level.
// the -40 is optional, can be changed to anything.
let pitchCamera = GMSPanoramaCameraUpdate.setPitch(CGFloat(motion!.attitude.pitch).radiansToDegrees - 40)
// rotation calculation (left/right)
let rotateCamera = GMSPanoramaCameraUpdate.rotate(by: -CGFloat(y))
// rotate camera immediately
panoView.updateCamera(pitchCamera, animationDuration: 0)
// for some reason, when trying to update camera
// immediately after one another, it will fail
// here we are dispatching after 1 millisecond for success
DispatchQueue.main.asyncAfter(deadline: .now() + 0.0001, execute: {
panoView.updateCamera(rotateCamera, animationDuration: 0)
})
}
})
}
}
}
extension BinaryInteger {
var degreesToRadians: CGFloat { return CGFloat(Int(self)) * .pi/180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi/180 }
var radiansToDegrees: Self { return self * 180/.pi }
}
而且不要忘了把這個在您的info.plist
Privacy - Motion Usage Description
添加一個適當的描述符,說明爲什麼你需要這些數據在info.plist中,並且應該正確配置你的應用程序。
你好,有關這個的任何答案? – mllm