如何理解应用旋转变换的UIView框架?

3

当我执行下面的代码时,遇到了一个奇怪的问题,这段代码是针对应用旋转变换的UIView编写的。我的目标是将该视图从其父视图中移除,然后将其插入到特定视图下方并恢复其框架:

  UIView* movedView=  [self.views objectAtIndex:newIndex];
    center = movedView.center;

   CGRect oldFrame  = movedView.frame;

    //find the layer on top and insert the moved view under that
    UIView* coveringView = [self.views objectAtIndex:newIndex-1];
    [movedView removeFromSuperview];
    [self.view insertSubview:movedView belowSubview:coveringView];
    movedView.frame = oldFrame;

我期望在操作后,视图应该出现在同一位置,但是它会根据通过CGAffineTransformMakeRotation()应用的旋转量而移动位置。

通过替换将frame赋值的调用为将center属性赋值的调用,我获得了期望的行为。这让我想到了一个问题 - 当UIView旋转时,我如何理解它的frame发生了什么变化?是否有好的教程或其他东西可以帮助我概念性地理解变换如何影响UIView的frame?

3个回答

4

- [UIView frame] 文档 中提到:

警告:如果 transform 属性不是恒等变换,则此属性的值是未定义的,因此应忽略它。

并且:

更改此属性可以进行动画处理。但是,如果 transform 属性包含非恒等变换,则不应修改 frame 属性的值,因为其值是未定义的。在这种情况下,您可以使用 center 属性重新定位视图,并使用 bounds 属性调整大小。

因此,如果您的 transform 不是恒等变换,最好避免考虑 frame 属性。


4
在使用CGAffineTransform之后,UIView的属性frame将不再被定义。文档中写道:“警告:如果变换属性不是标识变换,则此属性的值未定义,因此应将其忽略。”

0
在我看来,frame 是由 centertransformbounds 计算出来的。
就像这个公式:frame = f(center,transform,bounds)。但是我不知道 f(x) 的内部实现。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接