iOS传感器:使用陀螺仪完成一个小球撞壁的小游戏

if currentPoint.x <= imageWidth / 2 { currentPoint.x = imageWidth / 2 ballXVelocity = -ballXVelocity * 0.8 } if currentPoint.x >= bounds.size.width - imageWidth / 2 { currentPoint.x = bounds.size.width - imageWidth / 2 ballXVelocity = -ballXVelocity * 0.8 }

3.2.2 开启陀螺仪并更新

        manager.deviceMotionUpdateInterval = 1 / 60        //注意一下,在Swift没有了NSOperation。被OperationQueue取代了。        manager.startDeviceMotionUpdates(to: OperationQueue.main) { (motion, error) in            self.ballView!.accelleration = (motion?.gravity)!            //开启主队列异步线程,更新球的位置。            DispatchQueue.main.async {                self.ballView!.updateLocation(multiplier: 5000)            }

3.2.3 更新小球的位置