使用CGREct和UIView进行CGAffineTransformMakeScale的行为 [英] Behaviour of CGAffineTransformMakeScale with CGREct and with UIView

查看:162
本文介绍了使用CGREct和UIView进行CGAffineTransformMakeScale的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图定义为(0,0,320,480)。

我在此视图上调用转换:

I have a view with a frame defined as (0,0,320,480).
I call transformation on this view:

self.myView.transform = CGAffineTransformMakeScale(factor, factor); 

视图将按比例缩放,保留屏幕上的中心位置,以及我的更改后的框架(34,-8,251,376),你可以看到X和Y现在与0不同。

The view will scale preserving a central position on the screen and his frame after my changes will be for example (34,-8,251,376), as you can see X and Y are now different from 0.

如果我在带框架的CGRect上使用相同的功能(0,0,320,480) ):

If i use the same function on a CGRect with frame (0,0,320,480):

CGAffineTransform t = CGAffineTransformMakeScale(factor,factor);
CGRect rect2 = CGRectApplyAffineTransform(rect,t);

rect2将为X和Y保留0,我将获得类似的结果(0,0,251,376) )

rect2 will preserve 0 for X and Y and i'll obtain as result something like (0,0,251,376)

为什么rect2的X和Y不会像在UIView示例中那样改变?

Why X and Y for rect2 doesn't change as in UIView example ?

推荐答案

确实,你在技术上不应该在转换后查看UIView的frame属性,但它也不是技术上与您提出的问题相关。

It's true that you're not technically supposed to look at the frame property of a UIView after transformation, but it's also not technically pertinent to the question you're asking.

将CAffineTransforms应用于UIView时,转换会考虑UIView支持CALayer的anchorPoint属性。来自 CALayer 在anchorPoint上的文档:

When applying CAffineTransforms to a UIView, the transformation takes into consideration the UIView's backing CALayer's anchorPoint property. From the CALayer docs on anchorPoint:


默认为(0.5,0.5),
的中心界限矩形。

Defaults to (0.5, 0.5), the center of the bounds rectangle.

这意味着当您应用该缩放变换时,它会使用视图的中心作为锚点,因此视图围绕该位置缩放。我猜你是否要将锚点设置为(0,0),它的行为就像CGRect一样。

This means that when you apply that scale transform, it uses the center of the view as the anchor point, so the view scales around that location. I'm guessing if you were to set the anchor point to (0, 0), it would behave like CGRect does.

另一方面,CGRect是一个简单的C结构,并没有背衬层或锚点。因此行为上的差异。

CGRect, on the other hand, is a simple C struct, and doesn't have a backing layer or an anchor point. Thus the difference in behavior.

这篇关于使用CGREct和UIView进行CGAffineTransformMakeScale的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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