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
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

iphone shaking code

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?

- (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;
}

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
    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
    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
    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.
  • narendarnarendar Posts: 233Users Awaiting Email Confirmation
    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
    mek;20614 said:
    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.
    Index Card | Comic Viewer | <a href="http://click.linksynergy.com/fs-bin/stat?id=e*p82eCraXo&offerid
  • London_iPhone_DevLondon_iPhone_Dev Posts: 2New Users
    Is there any way of getting this in javascript form? I'm using phoneGap.

    Thanks in advance.
  • johnathonjohnathon Posts: 216Registered Users
    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
    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.