gpt4 book ai didi

ios - CGRectApplyAffineTransform 和实际 View 的框架

转载 作者:行者123 更新时间:2023-12-01 16:25:44 32 4
gpt4 key购买 nike

这是代码:

-(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}}

相同的矩形应用相同的变换,但得到不同的矩形,这就是为什么?

最佳答案

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.



在这种情况下,青年函数对每个点应用变换,并返回 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 的属性(property) 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.



由于您没有更改图层的 anchor ,因此从 View 中心应用变换。

原中心是 (60,60) .转换后的中心是 (60,140) ,因为缩放问题不会影响原点(即中心)。
您现在有一个 (80,80)(60,140) 为中心的矩形点:你可以
找到您的 {{20, 100}, {80, 80}}长方形。

关于ios - CGRectApplyAffineTransform 和实际 View 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475489/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com