视图之间坐标点的转换

本文档详细介绍了如何使用UIView的convertPoint:toView:和convertPoint:fromView:方法进行坐标转换。通过实例展示了如何在不同视图间转换点的坐标,帮助理解这些方法在视图坐标系统中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在转换一个点在两个不同的视图中的坐标时经常用到

UIView的方法

convertPoint:toView:

或者是

convertPoint:fromView:


[aView convertPoint:p toView:anotherView]是指将p相对aView的坐标转换为相对anotherView的坐标

比如说aView是原点(0,0), 宽高(1024, 768)的视图

anotherView是aView的子视图,原点是(100, 0),宽高(200, 200)

那么一个在anotherView中坐标是(0, 0)的点经过转换后在aView的坐标是(100, 0)

convertPoint:fromView:跟上面的方法是一个意思,只是转换的方向不一样而已


我这里把官方文档贴出来更直接点吧

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
Converts a point from the receiver’s coordinate system to that of the specified view.

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

Converts a rectangle from the coordinate system of another view to that of the receiver.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值