0

我正在创建一个应用程序,当用户摇动 iPhone 播放动画时,我正在使用以下代码:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)
{
    animation...
}

问题是,只要用户摇动 iPhone,上面的代码就会做出反应,只需一次。我需要用户上下摇晃它,就像他们在摇晃瓶子一样,持续几秒钟然后播放动画,同时他们摇动一个 mp3 文件必须播放,当动画播放时必须播放声音,请帮助,我是一个初学者,所以解释一下答案,谢谢:)

4

1 回答 1

2

Declare int numOfShakes; in your .h file and increment it everytime the user shakes their phone and once they reach a certain amount of shakes then call the animation code

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
        if (event.subtype == UIEventSubtypeMotionShake) {
            if (numOfShakes == 10) {
                //animate
                //Shook for a couple seconds
                numOfShakes = 0;
            } else {
                numOfShakes++;
                if (numOfShakes == 1) {
                //play some mp3
                }
            }
        }
    }
于 2012-07-15T15:04:28.987 回答