iOS frame转换问题(convertRect:toView)

//Converts a rectangle from the receiver’s coordinate system to that of another view

[view1 convertRect:view2.frame toView:view3]


注意:view1是view2的父视图,即view2的frame是相对于view1的(view1一般是scrollview)


view3这里认为是view1的父视图,可以得到滚动过程中view2相对于view3的frame


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

[view3 convertRect:view2.frame fromView:view1]这么写和上面代码的结果是一样的


 
 
(1) [self.view convertRect:(CGRect) fromView:(nullable UIView *)];

(2) [self.view convertRect:(CGRect) toView:(nullable UIView *)];

上面两行代码,不知道大家了解多少,是否会经常用到。也许你们用到的不多,但是肯定不会陌生,至少曾经在xxx个地方看见过,可一时半会儿又想不起来在哪里见过。于是乎,陷入了纠结ing......
  上网查资料,发现千篇一律,而且还比较抽象,不是很容易理解(也许有好的博客,只不过我没发现而已)。废话不多说,这里我用最笨的、也是最通俗易懂的方法来解释下,这两行代码具体的作用。若有不对之处,还望指正!
  这两行代码,均可划分为三部分,即:目标被操作的对象。fromView后面接的参数是:,toView后面接的参数是:目标,convertRect后面接的参数永远是:被操作的对象。作用是:计算上的被操作的对象相对于目标的frame。举个栗子:
��(1):

[viewB convertRect:viewC.frame toView:viewA];

该例子中显然viewA是目标,viewC是被操作的对象,那么剩下的viewB自然而然就是源了。作用就是计算viewB上的viewC相对于viewA的frame。
��(2):

[viewC convertRect:viewB.frame fromView:viewA];

该例子viewA是源,viewB是被操作的对象,那么viewC就是目标。作用就是计算viewA上的viewB相对于viewC的frame。



链接:http://www.jianshu.com/p/103d6aac84b4

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!感谢您的问题。下面是 Objective-C 实现的具体步骤: 1. 在控制器中创建一个 UIView,命名为 displayView,并设置其 frame 为控制器的 view 的 bounds。 ``` UIView *displayView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:displayView]; ``` 2. 在 displayView 上添加一个按钮,命名为 fullScreenButton。 ``` UIButton *fullScreenButton = [UIButton buttonWithType:UIButtonTypeSystem]; [fullScreenButton setTitle:@"全屏" forState:UIControlStateNormal]; fullScreenButton.frame = CGRectMake(0, 0, 100, 50); [fullScreenButton addTarget:self action:@selector(fullScreenAction:) forControlEvents:UIControlEventTouchUpInside]; [displayView addSubview:fullScreenButton]; ``` 3. 实现 fullScreenAction: 方法来控制 displayView 的横屏并全屏显示以及恢复竖屏显示并恢复原来尺寸。 ``` - (void)fullScreenAction:(UIButton *)sender { if (self.isFullScreen) { // 恢复竖屏显示并恢复原来尺寸 [UIView animateWithDuration:0.25 animations:^{ self.displayView.transform = CGAffineTransformIdentity; self.displayView.frame = self.view.bounds; }]; self.isFullScreen = NO; [sender setTitle:@"全屏" forState:UIControlStateNormal]; } else { // 横向并全屏显示 CGRect rectInWindow = [self.displayView convertRect:self.displayView.bounds toView:nil]; [UIView animateWithDuration:0.25 animations:^{ self.displayView.transform = CGAffineTransformMakeRotation(M_PI_2); self.displayView.frame = CGRectMake(0, 0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds)); self.displayView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); }]; self.isFullScreen = YES; [sender setTitle:@"恢复" forState:UIControlStateNormal]; } } ``` 在 fullScreenAction: 方法中,我们通过 CGAffineTransformMakeRotation() 方法来将 displayView 旋转90度,实现横向显示,并将其 frame 设置为横向全屏的尺寸和位置。在恢复竖屏显示时,我们通过 CGAffineTransformIdentity 将其旋转角度恢复为0,并且将其 frame 设置为竖屏的尺寸和位置。同时,我们通过一个布尔类型的变量 isFullScreen 来标记当前状态,以便下一次点击按钮时进行判断。 希望这个回答能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值