ios - Generating a UIColor with arguments tilt value data in swift -


i'm trying make app change colour of background based on tilt angle of device. i'm having no problem finding tilt values of device, can't seem use tilt values parameters in uicolor.

i have following code:

let manager = cmmotionmanager()  override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically 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)         }     }  } 

you think generate color varied depending on x-tilt of device, doesn't. type not supported.

does know how use "xcolor" variable change green level of background color?

the problem data.gravity.x returns double , uicolor expecting cgfloat value between 0.0 , 1.0. need convert double cgfloat , use abs() method make extract positive numbers negative ones.

import uikit import coremotion class viewcontroller: uiviewcontroller {     let motionmanager = cmmotionmanager()     override func viewdidload() {         super.viewdidload()         motionmanager.gyroupdateinterval = 0.1         motionmanager.startgyroupdates()         if motionmanager.devicemotionavailable {             motionmanager.devicemotionupdateinterval = 0.01             motionmanager.startdevicemotionupdatestoqueue(nsoperationqueue.mainqueue(), withhandler: { (data: cmdevicemotion!, error: nserror!) -> void in                 let x = data.gravity.x                 let y = data.gravity.y                 let z = data.gravity.z                 self.view.backgroundcolor = uicolor(                     red: cgfloat(abs(x)),                     green: cgfloat(abs(y)),                     blue: cgfloat(abs(z)),                     alpha: 1.0)             })         }     }     override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -