开发者

motionEnded works fine until I click a link in a webView

I'm using this code to do shake detection, and for the most part it works fine:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 if (event.type == UIEventSubtypeMotionShake) {
  if ( event.subtype == UIEventSubtypeMotionShake )
  {
   NSLog (@"Shake Called");
  }  
 }

I believe for this to work, I also needed to use these, which I have:

    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {
    [self resignFi开发者_JS百科rstResponder];
    [super viewDidDisappear:animated];
}

This all works great, and motionEnded is called every time I shake, until I click through a link in a webView on my program. I can scroll up and down in the view and shake still works, but as soon as the view loads a new page, motionEnded stops being called when I shake. This occurs both in the simulator and on a real device (iPhone 3G, iOS4). From my limited understanding, I believe it has something to do with first responders, but I could be wrong there!


I have managed to answer my own question. For the reference of others who may have similar problems, the issue is that the viewController stops being the first responder once you click a link in a webView (or edit a text box, or some similar action). To fix the issue in my case, the answer was to make the viewController the first responder again after the new page loads:

    - (void)webViewDidFinishLoad:(UIWebView *)nameOfWebView {
    [self becomeFirstResponder];
}


I have the same problem, but becoming first reponder when web view finishes loading doesn't solve it for me. I know webViewDidFinishLoad gets called when a new page has been loaded, and I also know motionEnded gets called when I focus the web view by tapping it and then shaking or simulating shake.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜