我正在开发一个 iOS 应用程序,我正在尝试旋转一个具有箭头图形的 UIImageView。我将房间中的位置作为 CGPoint,我想显示到达另一个 CGPoint 所需的方向。如何生成两点之间的角度,并确保箭头指向正确的方向,以及旋转 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;
问题是它只是让箭头疯狂跳跃,而不是从我的位置(yourLocation.x,yourLocation.y)指向原点(0,0)