1

I'm trying to make a shake events. I tried: 1) How do I detect when someone shakes an iPhone? (posts of Kendall, and Eran) 2) motionBegan: Not Working but nothig helps. My View becomes first responder, but motionBegan/motionEnded never called. Is there some additiol settings must be done, or i'm missing somethig? My iOS SDK is 4.3.

I have a class of UIView:

  #import "ShakeView.h"

    @implementation ShakeView
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
        NSLog (@"123");
        if ( event.subtype == UIEventSubtypeMotionShake ) {
            NSLog(@"Shake!");
        }

        if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
            [super motionEnded:motion withEvent:event];
        }
    }
    @end

In my ViewController's xib class of View is ShakeView. my ViewController pushed:

Wheel *secondViewController = [[Wheel alloc] initWithNibName:@"Wheel" bundle:nil];
    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];

In my ViewController:

- (void) viewDidAppear:(BOOL)animated
{
    [self.view becomeFirstResponder];
    [super viewWillAppear:animated];
    NSLog(@"%d", [self.view isFirstResponder]);
}
- (void) viewWillDisappear:(BOOL)animated
{
    [self.view resignFirstResponder];
    [super viewWillDisappear:animated];
}

It logs "1", so it IS first responder. But it logs nothing else.

I spend a half day on this few lines of code, and I have no more ideas. Do anyone knows how to solve it? Thanks.

Community
  • 1
  • 1
SentineL
  • 4,682
  • 5
  • 19
  • 38

1 Answers1

1

This is much too late to help SentineL, but I was having the same problem and I like his question because it is clear that he has all the relevant code in place -- except one crucial line, in the application delegate's didFinishLaunching:

[self.window makeKeyAndVisible];

This is very hard to debug, because even without this line, everything else will be fine. Your gestures will work, your controls will respond, you will be able to make your view first responder (as SentineL checked) -- but your subclassed window or view or view controller will never receive the motion events.

Which doesn't make sense to me. Why would makeKeyAndVisible affect the accelerometer but not gestures? Hopefully some more experienced user can answer that.

P.S. If you use this code as an example, I would recommend that you omit the super respondsToSelector conditional. Of course it responds to the selector; you're overriding it.

Wienke
  • 3,723
  • 27
  • 40