Cant figure out how to detect a Shake Motion **Help**

Discussion in 'iOS Programming' started by iJustinCabral, Aug 21, 2012.

  1. macrumors member

    iJustinCabral

    Joined:
    Jul 8, 2012
    #1
    My code right now is

    Code:
    
    -(void)viewWillAppear:(BOOL)animated
    {
    
    [super viewWillAppear:animated];
    
    [self becomeFirstResponder];
    
    }
    
    
    -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
    
    if (motion == UIEventSubtypeMotionShake)
    {
    [self.theList removeAllObjects];
    NSLog (@"You shook the device. New List Count %d",self.theList.count);
    }
    
    }
    
    -(void)motionEnded:(UIEventSubtype)motion withEvent(UIEvent *)event
    {
    NSLog(@"Device finished shaking");
    }
    
    
    Both NSLogs are not appearing in the debugger, so the device isn't getting this method sent to it.

    I thought i did everything right but the device still wont recognize the shake...any idea what I'm missing or doing wrong?
     
  2. macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    The object where you implemented these methods needs to be the "first responder" in order for UIEvents to be delivered to it.

    Make sure you've implemented canBecomeFirstResponder to return YES, and are calling becomeFirstResponder at some point.
     
  3. iJustinCabral, Aug 21, 2012
    Last edited by a moderator: Aug 24, 2012

    thread starter macrumors member

    iJustinCabral

    Joined:
    Jul 8, 2012
    #3
    Implemented

    Code:
    -(BOOL)canBecomeFirstResponder
    {
    return YES;
    }
    with the rest of my code and it worked perfect. Wonder why I missed that in the documentation.

    Thanks again reason!
     

Share This Page