Iphone sdk - moving a UIImageView with the Accelerometer? - iphone

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
}

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..

AR - Annotation to a fix Location

I made an augmented reality Application, there I have an arrow points on the z-axis to the location (longitude & latitude) of one shop now I'd like to make an annotation in the "sky" which points direct on the shop coordinate. I found some projects on github, like iPhone-AR-Toolkit, but I don't understand this.
Is there a simple way to add such an annotation above the location?
Edit:
After searching a lot I find a pdf document in de web, which contains some code for that what I want, but the problem is, I don't understand it and it doesn't do the things right.
The Code:
// Artificial Horizon - compensate for rotation along x-axis
// need to know the field of view of the camera to find horizon position inside of camera view
// about 53 degrees vertical and 37.5 degrees horizontal
// for directional Horizon - artificial horizon pegged to a specific cardinal direction (North)
// in (void)viewDidAppear
// Load the image to show in the overlay
UIImage *overlayGraphic = [UIImage imageNamed:#"Punkt.png"];
overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(30, 100, 50, 50);
[self addSubview:overlayGraphicView];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
locationManager = [[CoreLocationMangager alloc] init];
[locationManager setDelegate:self];
[locationManager startUpdatingHeading];
- (void) updateUI {
CGPoint overlayCenter = [overlayGraphicView center];
overlayCenter.y = 240.0 - 537.8 * sin(vertAngle);
overlayCenter.x = 160.0 - 497.8 * sin((magCompassHeadingInDeg) * (M_PI / 180.0));
[overlayGraphicView setCenter:overlayCenter];
overlayGraphicView.transform = CGAffineTransformMakeRotation(angle);
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
vertAngle = -atan2(acceleration.y, acceleration.z) - M_PI/2.0;
vertAngleInDeg = vertAngle * 180.0f/M_PI;
angle = -atan2(acceleration.y, acceleration.x) - M_PI/2.0;
angleInDeg = angle * 180.0f / M_PI;
[self updateUI];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
magCompassHeadingInDeg = [newHeading magneticHeading];
[self updateUI];
}
There is an image View is added to a camera overlay, and this image should point to North - but my problem is it also points to South. How can i fix that? And can anybody explain me thr values from the overlayCenter.y and .x - I don't understand them.
You need to Specify an altitude to specify the height from sea-level that is z-axis.
Mixare SDK will allow you to specify and load your POI's with latitude, longitude and altitude.
You can refer it from here
I have used this sdk and it works great and allows you specify Altitude.
Hope this helps:)

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

Iphone SDK: move image around with the accelerometer

Im trying to move an image around with the accelerometer by doing that:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
image.center = CGPointMake(acceleration.x, acceleration.y);
}
When i test the app, the image that is supposed to move around just sits in the x0 y0 position.
I declared the accelerometer, called the .h UIAccelerometerDelegate and so on...
What am i doing wrong?
Thanks in advance! -DD
You do realize that the accelerometer returns, as the name would suggest, measures of acceleration not points on the display? Anyway, what you need to do, is alter the center (not replace it completely), which will allow you to move the image.
Something along these lines:
image.center = CGPointMake(image.center.x + acceleration.x,
image.center.y - acceleration.y);
It is also important to note that the acceleration usually stays between -1 and 1 (unless the user shakes the device), which is due to the gravity being 1G. Therefore you should probably multiply the acceleration.x and .y values with some constant to make the image move a bit faster than about 1 point at a time.
There are additional things you should think about, what if the image is at the edge of the screen? What if the user wants to use the app in some other position than flat on a surface (needs calibration of the accelerometer)?
-(void)moveImage:(id)sender
{
[operationView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
[imgDeleteView setHidden:FALSE];
}
else if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
[imgDeleteView setHidden:TRUE];
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[(UIPanGestureRecognizer *)sender view] setCenter:translatedPoint];
}

iphone accelerometer: problem rotating image

Hi everyone I'm french so scue me for my english. So I have a problem.What I'm doing is when I turn device to left or right with accelerometer an image rotate in the opposite direction of the rotation of the device it create a cool effect but when I rotate the device with accelerometer the image is rotating but it is always trembling, vibrating the movement is not smooth. What can I do ? here is the code:
#import "QuartzCore/QuartzCore.h"
#define CONST_fps 100.
#define CONST_map_shift 0.05
#implementation MapViewRotationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// accelerometer settings
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / CONST_fps)];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static CGFloat ZZ = 0.;
CGFloat z = (atan2(acceleration.x, acceleration.y) + M_PI);
if (fabsf(ZZ - z) > CONST_map_shift)
{
viewToRotate.layer.transform = CATransform3DMakeRotation(ZZ=z, 0., 0., 10.);
}
}
#end
You need to do a trick that is called high pass filter. It filters trembling, only significant changes will pass.
You can google it, or search on StackOverflow. For example, here: How do you implement a Highpass filter for the IPhone accelerometer?