问题描述:

print and func call is successful both time, but my background and title change only when i restart my app and works only 1 times. Here is my code:

 var motionManager = CMMotionManager()

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)

startMotionManager()

}

func startMotionManager() {

if motionManager.accelerometerAvailable {

motionManager.accelerometerUpdateInterval = 0.2

motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {

[weak self] (data, error) in

print(data!.acceleration.z)

if(data!.acceleration.z > 0.8) {

self!.win()

print("Win")

}

if (data!.acceleration.z < -0.9){

self!.pass()

print("Pass")

}

return

})

}

}

func pass() {

print("passfunction")

self.testLabel.text = "PASS"

self.view.backgroundColor = UIColor.redColor()

}

func win() {

testLabel.text = "WIN"

view.backgroundColor = UIColor.greenColor()

}

}

My main goal is call func Win when acceleration.z > 0.8 which change the title label to "Win" and view background to green

If acceleration.z < -0.9 then call Pass function which change my background to red and etc...

Thanks for your help!

网友答案:

I tired all day solve this. After I posted my question I found the problem in my code.

    func pass() {
       NSOperationQueue.mainQueue().addOperationWithBlock {

       self.testLabel.text = "PASS"
       self.view.backgroundColor = UIColor.redColor()
       }
    }
相关阅读:
Top