ios坐标系的转换方法解析

31 篇文章 1 订阅

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;

- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;

- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;

- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

对于convertRect的参数具体可以归纳为:源、被操作对象、目标对象;(本质就是计算源上的被操作对象,相对于目标的frame)

fromView后面跟的参数:源

toView后面的参数是:目标

converRect后面跟的参数永远是被操作的对象;

例如:

[viewA convertRect:viewC.frame toView:viewB] ;  分析:viewB为目标对象,viewC是操作对象,那么源就是viewA(计算viewA上的viewC,相对于viewB的frame)

[viewA convertRect:viewC fromView:viewB];分析: viewB为源对象,viewC是被操作对象,那么目标对象就是viewA(计算viewBC上的viewC,相对于ViewA的frame)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 高德地图 SDK 中,可以使用 `MAMapView` 对象进行地图的显示和交互。该对象提供了一些方法可以实现坐标转换屏幕像素的功能。 以下是将地图上的经纬度坐标转换为屏幕像素的示例代码: ```objective-c // 初始化一个 MAMapView 对象 MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds]; // 创建一个 CLLocationCoordinate2D 类型的坐标 CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39.908692, 116.397477); // 将经纬度坐标转换为屏幕像素 CGPoint point = [mapView convertCoordinate:coordinate toPointToView:self.view]; NSLog(@"Screen point: %@", NSStringFromCGPoint(point)); ``` 以上代码中,`convertCoordinate:toPointToView:` 方法可以将地图上的经纬度坐标转换为在当前视图中的屏幕像素坐标。其中 `toPointToView:` 参数表示需要转换到的视图对象,这里使用 `self.view` 表示当前视图。 除了将经纬度坐标转换为屏幕像素,还可以使用 `convertPoint:toCoordinateFromView:` 方法将屏幕像素坐标转换为地图上的经纬度坐标。示例代码如下: ```objective-c // 创建一个 CGPoint 类型的屏幕像素坐标 CGPoint point = CGPointMake(100, 200); // 将屏幕像素坐标转换为地图上的经纬度坐标 CLLocationCoordinate2D coordinate = [mapView convertPoint:point toCoordinateFromView:self.view]; NSLog(@"Coordinate: %f, %f", coordinate.latitude, coordinate.longitude); ``` 以上代码中,`convertPoint:toCoordinateFromView:` 方法可以将屏幕像素坐标转换为地图上的经纬度坐标。其中 `toCoordinateFromView:` 参数表示需要转换的视图对象,这里同样使用 `self.view` 表示当前视图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值