Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Olivier Desmoulin
5,258 PointsProblem detecting the end of a shake event when testing on my device
Hi Guys,
I tested the cristalball code on my device and sometimes, while shaking the device, the action of setting the predictionLabel to nil works well, but the action of setting it to a new value does not work. It's like if the following method is not called. - (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if ( motion == UIEventSubtypeMotionShake ){ [self makePrediction]; }
Does that mean that some motion events that I could interpret as shake motions are not considered as shake motions by the device? How can I detect the ending of these motion events?
1 Answer
Amit Bijlani
Treehouse Guest TeacherThere are two scenarios:
Either it is assuming that you cancelled the shake event. In which case you should add a
NSLogto themotionCancelledevent.Or it is not registering as a shake event. In which case I would add NSLogs inside and outside the if statement
if ( motion == UIEventSubtypeMotionShake ).
Olivier Desmoulin
5,258 PointsOlivier Desmoulin
5,258 PointsHi Amit,
thank you very much for your answer. Following your advices, I added the NSLogs inside the motionCancelled and inside and outside the if condition of the motionEnded event and actually, I noticed that my problem hapens when the device does not detect the end of the shaking event. After a short period of time, it considers it as a Cancelled motion. For this reason I added a call to the makePrediction method inside the motionCancelled event, so that each time I shake the device, it can make a prediction, even if it does not detect the end of the motion. Do you think this is an acceptable solution? Or do you have a better recommendation?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherI don't think that is the right approach because you don't want a cancel event to fire a prediction. From what you wrote above it sounds like you might be shaking the device for too long and it finally registers it as cancelled.
Here's what Apple's documentation has to say:
Olivier Desmoulin
5,258 PointsOlivier Desmoulin
5,258 PointsOK, Thank you Amit for your answer. best, Olivier