IPhone : Detect constant shake

on Monday, February 23, 2015

I'm trying to count shakes when users shake their phones up and down.



override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if motion == .MotionShake {
count++
self.shakeLabel.text = String(count)

}
}


I tried the code above and it works but it only reacts to a single shake. If I shake my phone rigorously, count doesn't go up. I also tried motionBegan with no luck.


After some research, it looked like I needed to work with the accelerometer data. I tried the codes from this website http://www.ioscreator.com/tutorials/move-sprites-accelerometer-spritekit-swift with minor tweaks. It reacts to gravity and moves the object up and down but it does not react to shakes.


How can I achieve what I want? I'm trying out swift for the first time but I can go back to obj-c if you know the answer in obj-c.


0 comments:

Post a Comment