问题
Possible Duplicate:
How to stop responding to a shake before a button is pressed?
I have a app and currently if you click the button 'Play' a label show's saying 'Shake to Play' the problem is I am a total beginner and I don't know how to order it so the user has to first press the button, then when the label show's, if the user shake's the label disappears. that's all I need because I have sorted out other parts of the app after that. Thanks to anyone who helps, remember I am quite new so please explain any answers :) thanks
回答1:
the obvious way to do this would be to create an iVar or @property, and in the action that occurs as a result of pressing the "Play" button, set the iVar/@property to YES. in the motionBegan:withEvent: where you detect the shake, simply return without performing the shake-action if the iVar/@property is not YES. and have set the label to hidden when you are accepting that the user has shaken at the appropriate time.
回答2:
Create an instance variable of type BOOL. Call it, say, playIsTouched. Set it to NO in the view controller's viewDidLoad method. In the method that handles touches to the Play button, set playIsTouched to YES. In the method that handles shakes, make the label disappear only if playIsTouched == YES. In the same method, you may want to set playIsTouched back to NO (if you want to user to touch the button again before shaking).
来源:https://stackoverflow.com/questions/11511984/how-to-motion-shake-only-after-a-button