I am working on an iOS app, and I am trying to rotate a UIImageView that has the graphic of an arrow. I have my location in the room as a CGPoint, and I want to show the direction you need to head to reach another CGPoint. How do I generate the angle between the two points, and make sure the arrow is pointed the correct direction, also what is the best way to rotate the UIImageView?
- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}
CGAffineTransform transform = CGAffineTransformMakeRotation([self pointPairToBearingDegrees:yourLocation secondPoint:CGPointMake(0, 0)]);
self.arrow.transform = transform;
The problem is that it just makes the arrow jump wildly instead of pointing to the origin (0,0) from my position (yourLocation.x,yourLocation.y)