2013-02-26 110 views
1

我有兩個點CLLocationCoordinate2D,比如說我的mapView上的A & B.我想要一個箭頭圖像指向一個點(A-> B)。我無法找到與transform箭頭圖像正確的角度。兩點座標之間的角度相應地旋轉圖像

請參閱此圖像以供參考提前example screen-shot

感謝。

+0

WT你必須嘗試? – iPatel 2013-02-26 07:03:23

+0

我覺得這是幾何101,在這裏。你有沒有嘗試做一點數學(確切地說是三角)?可能的欺騙:http://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-two-points-relative-to-the-horizo​​ntal-axis – CodaFi 2013-02-26 07:15:21

+0

感謝您的迴應,我有 ' float deltaLongitude = second.longitude - first.longitude; float deltaLatitude = second.latitude - first.latitude; float angle = atan2(deltaLongitude,deltaLatitude)* 180/M_PI; pointerView.transform = CGAffineTransformMakeRotation(angle);' 形象依然不好。 – Samapple007 2013-02-26 07:34:20

回答

3

這是一個老問題,但或許它仍然可以幫助別人尋找位置:

CGFloat angle = atan2f(point2.y - point1.y, point2.x - point1.x); 
CGAffineTransform rotationTransform = CGAffineTransformIdentity; 
rotationTransform = CGAffineTransformMakeRotation(angle); 
arrow.transform = rotationTransform; 

如果您想放置一個UIImageView「箭頭」點1,並將其指向2點,你只需使用deltas來獲得反正切,然後根據該角度創建一個變換,並將該變換分配給箭頭的UIImageView

+0

如何獲得此動畫? https://stackoverflow.com/q/46709254/3908884 – 2017-10-16 12:04:08