I would like to program a game at xcode in swift, but I need this because a certain object with a tilt sensor of the Handys should be controlled this. My question: If there is something like, how do I use this (tutorial)?
Tilt sensor with Xcode?
Look at CoreMotion, it has the API for reading accelerometer, gyro, attitude (hence tilt).
You have to import CoreMotion and access CMDeviceMotion! parameters such as motion.attitude
Have a look here
h ttps://developer.apple.com/reference/coremotion/cmmotionmanager
Hi,
Try out the following code:
import CoreMotion
import UIKit
class ViewController: UIViewController {
var motionManager: CMMotionManager!
override func viewDidLoad() {
super.viewDidLoad()
motionManager = CMMotionManager()
if (motionManager.isAccelerometerAvailable){
motionManager.startAccelerometerUpdates(
to: OperationQueue.current!,
withHandler: {(accelData: CMAccelerometerData?, errorOC: Error?) in
self.outputAccelData(acceleration: accelData!.acceleration)
})
}
if (motionManager.isGyroAvailable){
motionManager.startGyroUpdates(
to: OperationQueue.current!,
withHandler: { (gyroData: CMGyroData?, errorOC: Error?) in
self.outputGyroData(gyro: gyroData!)
})
}
}
func outputAccelData(acceleration: CMAcceleration){
print(acceleration.x)
print(acceleration.y)
print(acceleration.z)
}
func outputGyroData(gyro: CMGyroData){
print("Gyro rotation: \(gyro.rotationRate)")
}
}Have fun,
Joris