Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

iphone shaking code

mekmek Posts: 8New Users
edited February 2010 in iPhone SDK Development
iphone shaking code

ive looked around quite a bit, and can't seem to find any examples of code to perform an action if the iphone is shaken

has anyone been able to successfully do this? if so how
i would imagine you would monitor the accelerometer for changes in x,y,and,z

maybe?
<code>
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float earthquake = 1.5;
static BOOL shakeme;
BOOL shake = FALSE;

if (shakeme) return;
beenhere = TRUE;
if (acceleration.x > earthquake || acceleration.x < (-1* earthquake))
shake = TRUE;
if (acceleration.y > earthquake || acceleration.y < (-1* earthquake))
shake = TRUE;
if (acceleration.z > earthquake || acceleration.z < (-1* earthquake))
shake = TRUE;
if (shake) { /* perform action here */ }

shakeme = false;
}
</code>
so if the action that you wanted to perform was to pull a random object/data from a sqlite3 database, it would be some rand code that randomized the Primary key, and whatever that value is ...send that data to the view or a label or imagecontroller

i dunno

any ideas?
Post edited by mek on

Replies

  • pfhortepfhorte Posts: 14Registered Users
    edited September 2008
    This is what I do for a left to right shake.


    - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration;
    {

    ++frameNum;
    if (acceleration.x > shakeLimit && shakeLeft == NO)
    {
    lastFrameFullRight = frameNum;
    shakeLeft = YES;
    if (lastFrameFullRight - lastFrameFullLeft < 15) // a shake
    {
    if (++numChanges > 3)
    {
    [self erase];
    numChanges = 0;
    }
    }
    else
    numChanges = 0;
    }
    if (acceleration.x < -shakeLimit && shakeLeft == YES)
    {
    lastFrameFullLeft = frameNum;
    shakeLeft = NO;
    if (lastFrameFullLeft - lastFrameFullRight < 15) // a shake
    {
    if (++numChanges > 3)
    {
    [self erase];
    numChanges = 0;
    }
    }
    else
    numChanges = 0;
    }
    }
  • db3ddb3d Posts: 4New Users
    edited September 2008
    Check out how LightsOn did it. Most of it is on LightsOnApplication.m

    Essentially calculating if the distance from the last point was above a certain threshold. The distance is calculated by:

    #define DIST(X, Y, Z) sqrtf(((X)*(X))+((Y)*(Y))+((Z)*(Z)));

    And used in:

    dist = DIST(x - old_x, y - old_y, z - old_z);
  • pfhortepfhorte Posts: 14Registered Users
    edited September 2008
    That does not detect a shake, only a large singe movement.
    My code detects several large movements, first one way, then another and also the frequency of the movement.
    Shake too slow, and its NOT a shake. Shake only once and its NOT a shake.
  • mekmek Posts: 8New Users
    edited October 2008
  • narendarnarendar Posts: 233
    edited March 2009
    hi can you please tell
    what values you assigned for the variables at starting ...for

    lastFrameFullRight , frameNum, shakeLimit.

    Of course it is simple Question i am asking ....can you post it with total code.

    thansk,
    naren
  • DenVogDenVog Posts: 625Registered Users
    edited March 2009
    mek wrote: »
    ive looked around quite a bit, and can't seem to find any examples of code to perform an action if the iphone is shaken

    There is some sample code from the Beginning iPhone Development book that covers this. As I recall, it's Chapter 15 "ShakeandBreak" example. You can download the code even if you don't have the book, but I recommend the book if you're new to iPhone Dev.
  • London_iPhone_DevLondon_iPhone_Dev Posts: 2New Users
    edited November 2009
    Is there any way of getting this in javascript form? I'm using phoneGap.

    Thanks in advance.
  • johnathonjohnathon Posts: 216Registered Users
    edited November 2009
    Use the new motionEnded method in 3.0. Your view (or viewController) has to be first responder to recieve shake events.
    This should do it :
    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    -(void)viewDidAppear:(BOOL)animated {
    
        [self becomeFirstResponder];
    }
    
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    	if (event.type == UIEventSubtypeMotionShake) {
                //It has sheked
    
    	}
    }
    
  • HokieHokie Posts: 2New Users
    edited February 2010
    Personally I have tried out the - (void)motionEnded...method and found the response time to be rather slow.

    What I am trying to do is detect a fast shake vs. a slow shake and write a message. The project compiles, and if you shake below the 1.5 acceleration threshold I setup the labels says "Slow" as it is suppose to. Speed up the shake and it shows "Fast." THE PROBLEM is if I slow the shake back down the label doesn't change. What I have is as follows:

    - (void)accelerometer : (UIAccelerometer *)accelerometer
    didAccelerate : (UIAcceleration *)acceleration {

    static NSInteger shakeCount = 0;
    static NSDate *shakeStart;

    NSDate *now = [[NSDate alloc] init];
    NSDate *checkDate = [[NSDate alloc] initWithTimeInterval: 2.0f sinceDate:shakeStart];

    if ([now compare:checkDate]== NSOrderedDescending || shakeStart == nil) {

    shakeCount = 0;
    [shakeStart release];
    shakeStart = [[NSDate alloc] init];
    }

    [now release];
    [checkDate release];

    // Shake is too fast
    if (acceleration.y > AccelerationThreshold) {
    shakeCount++;

    if (shakeCount > 4) {
    myLabel.text = [NSString stringWithFormat:@"To Fast %i", shakeCount];
    NSLog(@"To Fast");
    shakeCount = 0;
    shakeStart == nil;
    [shakeStart release];
    shakeStart = [[NSDate alloc] init];
    // shakeCheckComplete = YES;
    }

    }



    // Shake is slow
    if (acceleration.y < AccelerationThreshold && acceleration.y > 0.5) {

    shakeCount++;

    if (shakeCount > 5) {
    myLabel.text = [NSString stringWithFormat:@"Slow %i", shakeCount];
    NSLog(@"Slow");
    shakeCount = 0;
    shakeStart == nil;
    [shakeStart release];
    shakeStart = [[NSDate alloc] init];
    }
    }




    }
Sign In or Register to comment.