CGRectApplyAffineTransform 和实际视图的框架
CGRectApplyAffineTransform and actual view's frame
代码如下:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView* view=[[UIView alloc] init];
CGRect frame=CGRectMake(10, 10, 100, 100);
view.frame=frame;
[self.view addSubview:view];
CGAffineTransform t1 = CGAffineTransformMakeTranslation(0, 100);
CGAffineTransform t2 = CGAffineTransformMakeScale(.8, .8);
CGAffineTransform t3 = CGAffineTransformConcat(t1, t2);
view.transform=t3;
CGRect rect = CGRectApplyAffineTransform(frame, t3);
NSLog(@"transform rect:%@", NSStringFromCGRect(rect));
NSLog(@"transform view rect:%@", NSStringFromCGRect(view.frame));
}
//output:
transform rect:{{8, 88}, {80, 80}}
transform view rect:{{20, 100}, {80, 80}}
同一个矩形应用相同的变换,但得到不同的矩形,这就是为什么?
UIView 的文档为框架说明了这一点 属性
Warning: If the transform
property is not the identity transform,
the value of this property is undefined and therefore should be
ignored.
在 CGRect
或 UIView
对象上应用仿射变换是有区别的:
先从CGRectApplyAffineTransform
开始,再看Apple docs的描述:
Because affine transforms do not preserve rectangles in general, the
function CGRectApplyAffineTransform returns the smallest rectangle
that contains the transformed corner points of the rect parameter. If
the affine transform t consists solely of scaling and translation
operations, then the returned rectangle coincides with the rectangle
constructed from the four transformed corners.
在这种情况下,青年函数对每个点应用变换,并且returns一个CGRect
对象包含所有这些点。
t(10,10) ->(8,88)
t(10,110)->(8, 168)
t(110,10)->(88, 88)
t(110,110)->(88, 168)
包含所有这些转换点的矩形是正确的{{8, 88}, {80, 80}}
现在让我们看看 transform
属性 来自 UIView
documentation 的描述:
The origin of the transform is the value of the center property, or the layer’s anchorPoint property if it was changed. (Use the layer property to get the underlying Core Animation layer object.) The default value is CGAffineTransformIdentity.
由于您没有更改图层的锚点,因此将从视图中心应用变换。
原来的中心是(60,60)
。变换后的中心是 (60,140)
,因为缩放问题不影响原点(即中心)。
您现在有一个以 (60,140)
点为中心的 (80,80)
矩形:您可以
找到你的 {{20, 100}, {80, 80}}
矩形。
代码如下:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView* view=[[UIView alloc] init];
CGRect frame=CGRectMake(10, 10, 100, 100);
view.frame=frame;
[self.view addSubview:view];
CGAffineTransform t1 = CGAffineTransformMakeTranslation(0, 100);
CGAffineTransform t2 = CGAffineTransformMakeScale(.8, .8);
CGAffineTransform t3 = CGAffineTransformConcat(t1, t2);
view.transform=t3;
CGRect rect = CGRectApplyAffineTransform(frame, t3);
NSLog(@"transform rect:%@", NSStringFromCGRect(rect));
NSLog(@"transform view rect:%@", NSStringFromCGRect(view.frame));
}
//output:
transform rect:{{8, 88}, {80, 80}}
transform view rect:{{20, 100}, {80, 80}}
同一个矩形应用相同的变换,但得到不同的矩形,这就是为什么?
UIView 的文档为框架说明了这一点 属性
Warning: If the
transform
property is not the identity transform, the value of this property is undefined and therefore should be ignored.
在 CGRect
或 UIView
对象上应用仿射变换是有区别的:
先从CGRectApplyAffineTransform
开始,再看Apple docs的描述:
Because affine transforms do not preserve rectangles in general, the function CGRectApplyAffineTransform returns the smallest rectangle that contains the transformed corner points of the rect parameter. If the affine transform t consists solely of scaling and translation operations, then the returned rectangle coincides with the rectangle constructed from the four transformed corners.
在这种情况下,青年函数对每个点应用变换,并且returns一个CGRect
对象包含所有这些点。
t(10,10) ->(8,88)
t(10,110)->(8, 168)
t(110,10)->(88, 88)
t(110,110)->(88, 168)
包含所有这些转换点的矩形是正确的{{8, 88}, {80, 80}}
现在让我们看看 transform
属性 来自 UIView
documentation 的描述:
The origin of the transform is the value of the center property, or the layer’s anchorPoint property if it was changed. (Use the layer property to get the underlying Core Animation layer object.) The default value is CGAffineTransformIdentity.
由于您没有更改图层的锚点,因此将从视图中心应用变换。
原来的中心是(60,60)
。变换后的中心是 (60,140)
,因为缩放问题不影响原点(即中心)。
您现在有一个以 (60,140)
点为中心的 (80,80)
矩形:您可以
找到你的 {{20, 100}, {80, 80}}
矩形。