如何在转换之前计算UIView的框架? [英] How to compute the frame of a UIView before the transform?

查看:102
本文介绍了如何在转换之前计算UIView的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,我向它申请了CGAffineTransform.我希望能够了解如果未应用转换的视图框架.

I have a UIView and I apply to it a CGAffineTransform. I would like to be able to know the frame of the view if the transform had not be applied.

我正在寻找一种(简单的)解决方案来处理变换为零变换"(不可逆)的情况.

I am looking for a (simple) solution that handles the case where the transform is the "zero-transform" (which is not invertible).

我认为以下内容不适用于零变换".

The following does not work for the "zero-transform", I guess.

CGRect result ;

CGAffineTransform transform = self.transform ;

result = CGRectApplyAffineTransform(self.frame, CGAffineTransformInvert(transform)) ;

return result ;

为什么?

我需要对动画执行此操作

Why?

I need to do this for an animation:

  • 初始设置:我将transform设置为"0".插入了视图,但是所有元素的框架都像视图不在这里一样.
  • 最终设置:我计算所有元素的框架,这次我考虑了新视图.我将其transform设置为"1".
  • initial settings: I set the transform "to 0". The view is inserted but the frames of all the elements do as if the view was not here.
  • final settings: I compute the frames of all my elements, and this time I take into account the new view. I set its transform "to 1".

我的变换是比例变换.

推荐答案

transform不会影响centerbounds.

因此,您可以通过以下简单代码进行计算:

So, you can calculate it by this simple code:

    CGPoint center = view.center;
    CGSize size = view.bounds.size;

    CGRect frameForTransformIdentity = CGRectMake(
        center.x - size.width / 2,
        center.y - size.height / 2,
        size.width,
        size.height
    );

这篇关于如何在转换之前计算UIView的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆