ios - CGRectApplyAffineTransform 和实际 View 的框架

标签 ios core-graphics core-animation

这是代码:

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

相关文章:

ios - 如何镜像 UIBezierPath?

objective-c - 如何同步动画 UIView 和 CALayer

ios - didSelectRowAtIndexPath 之后的延迟

ios - 了解 iOS 崩溃日志

ios - 将按钮添加到单元格

ios - 顶部/底部、左/右边缘有白线的 PDF 渲染

ios - 你如何在 Swift 中锁定一个线程?

ios - drawRect 形状周围的黑框 swift

ios - 闪光效果 iOS

ios - Core Animation 交互式翻转过渡像扑克牌一样有正面和背面