Search This Blog

Jul 13, 2010

How to create a Virtual Compass in iPhone app.

#define RadiansTodegrees(Y) ( 180.0* Y / 3.14)


IBOutlet UIImageView *arrow;
IBOutlet UIImageView *Compass;
CGPoint theEndPoint;
CGPoint theStartPoint;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
UITouch *touch = [touches anyObject];
theEndPoint = [touch locationInView:self.view];
if((theEndPoint.x>Compass.frame.origin.x && theEndPoint.y>Compass.frame.origin.y) && (theEndPoint.x<Compass.frame.origin.x+Compass.frame.size.width && theEndPoint.y<Compass.frame.origin.y+Compass.frame.size.height))
{
float Centre_X = Compass.frame.origin.x + (Compass.frame.size.height/2);
float Centre_Y = Compass.frame.origin.y + (Compass.frame.size.width/2);
float dx=theEndPoint.x-Centre_X;
float dy=theEndPoint.y-Centre_Y;
double angle=atan2(dy,dx);
double br = RadiansTodegrees(angle);
CGAffineTransform rotate = CGAffineTransformMakeRotation(((br+90)/180)*3.14);
[arrow setTransform:rotate];
}
}

No comments:

Post a Comment