[Help] Detecting Shake in MotionBegan

Discussion in 'iOS Development' started by cocotutch, Jul 28, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    This seems to be impossible. I am using a View Controller, and I want the shake to be received by my Main Menu View and have a button click ([self play

    Please Register or Log in to view images

    LAYBUTTON]

    Please Register or Log in to view images

    and have numerous other functions complete on the shake. But since these methods seem to not work in a View Controller and the old shaking code doesn't even work in the Simulator (yes GLPaint too), I am at my wits end here, I have spent longer on this than anything I have done so far.

    Please help,

    cocotutch
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I read somewhere that the view (in your case "Main Menu View") must be set as the FirstResponder.

    Not entirely sure, however.
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You need to tell your view controller to conform to the <UIAccelermoeterDelegate> protocol. Then somewhere, call [OBJC]
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:someIntervalInHz];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];[/OBJC]

    If you're using a nib, use initWithCoder, else initWithNib:etc: if you want that in the initialization method. Try loadView or viewDidLoad: if you want to set up those values at load time.

    Next, implement the UIAccelerometerDelegate method
    [OBJC]- (void)accelerometer

    Please Register or Log in to view images

    UIAccelerometer *)accelerometer didAccelerate

    Please Register or Log in to view images

    UIAcceleration *)acceleration; [/OBJC]

    UIAcceleration has x, y, and z properties, one for each respective axis. Use geometry to calculate the length of the shake along the x, y, and z axes. Store that value (a float) in an Ivar called lastLength (or whatever).

    Now, take acceleration.x, acceleration.y, acceleration.z. If lastLength != 0, and length - lastLength > someValueYouDetermineToBeAnAcceptableShakeDistance, then you have just detected the shake.

    In your situation because this is all within a view controller, you will call [self play:nil];
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have tried that Skylar, but thanks anyway. It was the code i originally had and it was from GLPaint. It seems that there is a new API -- motionBegan, motionCancelled, motionEnded --. I'm trying to implement these into my view controller without much luck at all. Maybe I could subclass my UIView for the MainMenu to see if it would work?
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    What I told you is exactly how I detect shakes in PocketTouch. This has worked flawlessly in a UIView (1.x), UIViewController (2.0 - 3.1b3) and the UIApplicationDelegate (3.0b3). I don't know what the root problem us that is causing this not to work for you.

    Try to put an NSLog into the accelerationDidAccelerate to see if it even makes it that far. Then from there, we can work out the issue.

    I am aware of the shaking not working in the Simulator, but I think that is a recently introduced Simulator issue.
  6. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I ended up getting the shaking working with the old code. But then the ball stopped responding to the accelerometer, and then I realized that the Ball was utilizing the hardware and if the Menu used it, the ball would give up the ghost. So I had to take it out, looks like Swipes and Rotation is the go here

    Please Register or Log in to view images

Share This Page