Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-27-2008, 07:08 PM   #1 (permalink)
mek
New Member
 
Join Date: Sep 2008
Posts: 8
mek is on a distinguished road
Default 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?
<code>
- (void)accelerometerUIAccelerometer *)accelerometer didAccelerateUIAcceleration *)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?
mek is offline   Reply With Quote
Old 09-27-2008, 11:41 PM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 14
pfhorte is on a distinguished road
Default

This is what I do for a left to right shake.


- (void)accelerometerUIAccelerometer*)acceleromete r didAccelerateUIAcceleration*)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;
}
}
pfhorte is offline   Reply With Quote
Old 09-28-2008, 01:08 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 4
db3d is on a distinguished road
Default Example on Google code

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);
db3d is offline   Reply With Quote
Old 09-28-2008, 01:13 PM   #4 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 14
pfhorte is on a distinguished road
Default

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.
pfhorte is offline   Reply With Quote
Old 10-01-2008, 09:21 PM   #5 (permalink)
mek
New Member
 
Join Date: Sep 2008
Posts: 8
mek is on a distinguished road
Default

thank you so much for that
mek is offline   Reply With Quote
Old 03-23-2009, 11:34 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Hyderabad,India
Age: 25
Posts: 233
narendar is on a distinguished road
Default

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
narendar is offline   Reply With Quote
Old 03-23-2009, 01:23 PM   #7 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Default

Quote:
Originally Posted by mek View Post
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.
DenVog is offline   Reply With Quote
Old 11-19-2009, 11:39 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 2
London_iPhone_Dev is on a distinguished road
Smile I'm a newbie

Is there any way of getting this in javascript form? I'm using phoneGap.

Thanks in advance.
London_iPhone_Dev is offline   Reply With Quote
Old 11-19-2009, 02:32 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 216
johnathon is on a distinguished road
Default

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 :

Code:
-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {

    [self becomeFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	if (event.type == UIEventSubtypeMotionShake) {
            //It has sheked

	}
}
johnathon is offline   Reply With Quote
Old 02-19-2010, 10:21 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 2
Hokie is on a distinguished road
Default Detecting a Slow Shake Vs. Fast Shake

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




}
Hokie is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 347
11 members and 336 guests
akacaj, c2matrix, cgokey, esoteric, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,651
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Mirotion22
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0