我正在尝试制作一个应用程序,它将根据设备的倾斜角度改变背景颜色。我找到设备的倾斜值没有问题,我似乎无法将倾斜值用作 UIColor 中的参数。
我有以下代码:
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager.gyroUpdateInterval = 0.1
manager.startGyroUpdates()
if manager.deviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] (data: CMDeviceMotion!, error: NSError!) in
let xColor = data.gravity.x
self!.view.backgroundColor = UIColor(red: 155/255, green: xColor, blue: 219/255, alpha: 1)
}
}
}
您可能会认为它会根据设备的 x 倾斜度生成不同的颜色,但事实并非如此。不支持该类型。
Does anybody know how I could use the "xColor" variable to change the green level of the background color?