How to detect when iPhone is moving in X direction? - iphone

How do I detect that the iPhone is moving in a particular direction (X-axis)? Can you show some sample code?

You can do it with help of UIAccelerometer and check the movement fo phone in x-axis by:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)aceler {
if (sqrt(aceler.x * aceler.x) > 1.5f)
{
//write your code here
}
}
Hope this helps.
Thanks,
Madhup

Related

Device motion detection in iOS

I want to detect the device motion like Panorama i.e. motion from my left side to Right side.
I tried with the accelerometer but i can't able to do it.
I like to move one image in a straight line as the device move.
My code is as follow:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
[[UIAccelerometer sharedAccelerometer]setDelegate:self];
}
pragma mark - Accelerometer Delegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
(UIAcceleration *)acceleration{
valueX = acceleration.x*100.0;
valueY = acceleration.y*30.0;
float newX = (float)(ball.center.x + valueX);
CGPoint newCenter = CGPointMake(newX, 50);
ball.center = newCenter;
}
How can I do this? Because there are many apps who uses this.
Please guide me.
Thanks..

Different Images using Accelerometer in Xcode

I am trying to make an app in Xcode 4.3.3 where one image is displayed when the z-axis using the accelerometer is positive. But, when the z-axis becomes negative, I would like a different image to appear. I'm a beginner, so if you could tell me the file in which I should write the code in that would be great.
Thank you!
Check out below links to understand UIAccelerometer
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAccelerometer_Class/Reference/UIAccelerometer.html
http://homepages.ius.edu/rwisman/C490/html/chapter22.htm
http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-accelerometer-in-iphone/
http://www.ifans.com/forums/threads/tutorial-simple-accelerometer-source-code.151394/
You would require to make changes in following function
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
if(acceleration.z<0) {
imageview.image = //Display image to be displayed when z is negative
}
else {
imageview.image = //Display image to be displayed when z is positive
}
}

box2d accelerometer how to make it work

I'm trying to make the box2d accelerometer work, I have a car sprite and want it to move left and right, when the iPhone is tilted.
Here is the code for the sprite:
- (void)spawnCar {
car = [CCSprite spriteWithSpriteFrameName:#"car.jpg"];
car.position = ccp(160, 250);
car.tag = 2;
[self addBoxBodyForSprite:car];
[_spriteSheet addChild:car];
}
How can implement the accelerometer to work for left and right?
Just do this...
in your init add
self.isAccelerometerEnabled = YES;
and then add this method...
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
b2Vec2 gravity(-acceleration.y * 15, acceleration.x *15);
world->SetGravity(gravity);
}

Moving a cocos2d-iphone+box2d boy in one direction

Please can someone advice me on this.
I have a cocos2d-iphone +box2d body I wish to move only in one direction(movement only from left to right of the iphone screen) using only the accelerometer controls and parallax scrolling. Please how do I do this as at the moment the body moves both to the left and right once the device is tilted. I do not want to use forces or impulse since I will NOT be enabling touch controls.
This is my accelerometer code :
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration
*)acceleration {
float32 accelerationFraction = acceleration.y * 6;
if (accelerationFraction < -1) {
accelerationFraction = -1;
} else if (accelerationFraction > 1) {
accelerationFraction = 1;
b2Vec2 gravity(-acceleration.y *10, acceleration.x *10);
world->SetGravity( gravity );
}
Thanks in advance for your help .
You should use mouseJoints. Look at this tutorial. There he explains exactly what you are trying to accomplish.

Iphone sdk - moving a UIImageView with the Accelerometer?

I haven't had any experience with the accelerometer yet and i was wondering how i would move my UIImageView with the accelerometer, can anyone point me to any tutorials or give me a little help?
thanks,
harry
There are a collection of tutorials here http://www.iphonedevforums.com/forum/sdk-coding-help/854-how-use-accelerometer.html which should meet your needs.
//viewDidLoad
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
//then
- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler {
//move your image here
//based on aceler.x aceler.y aceler.z
}