iOS获取点击的点在屏幕上的坐标,获取某个view在屏幕上的位置

1.获取点击事件的点相对于屏幕的位置

如果点击事件是如下这个方法(将UIEvent当做参数传回来)

-(void)expandButtonClicked:(id)sender withEvent:(UIEvent*)event

那么点击的位置相对于屏幕的坐标就是:

UITouch* touch = [[event touchesForView:btn] anyObject];
CGPoint rootViewLocation = [touch locationInView:[FtAppDelegate shareAppDelegate].rootViewCtrl.view];
复制代码
2.如果想知道一个view相对于屏幕或者另外一个view 的坐标,那么可以通过如下的方法得到:
UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
CGRect rect=[bView convertRect: bView.bounds toView:window];
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值