CGAffineTransform改变了view.bounds?

时间:2012-07-02 05:25:06

标签: ios frame cgaffinetransform bounds

  

视图有一个框架(superview的坐标系中的坐标)   和边界(自己的坐标系中的坐标)属性,但如果你   转换视图,不应该使用或依赖于frame属性   了。如果您正在使用转换,请使用边界   属性,而不是框架属性,因为应用了转换   到界限,但不一定在框架中准确反映

http://iphonedevelopment.blogspot.jp/2008/10/demystifying-cgaffinetransform.html

我想在上面的段落中看到他的意思并打印出“框架”和“边界” 我看到只有'框架'在捏合期间发生变化。

- (IBAction)handlePinch:(UIPinchGestureRecognizer*)recognizer
{
    NSLog(@"scale: %f, velocity: %f", recognizer.scale, recognizer.velocity);
    NSLog(@"Before, frame: %@, bounds: %@", NSStringFromCGRect(recognizer.view.frame), NSStringFromCGRect(recognizer.view.bounds));
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    NSLog(@"After, frame: %@, bounds: %@", NSStringFromCGRect(recognizer.view.frame), NSStringFromCGRect(recognizer.view.bounds));

    recognizer.scale = 1;
}

输出:( zomming in)

2012-07-02 14:53:51.458 GestureRec[1264:707] scale: 1.030111, velocity: 0.945660
2012-07-02 14:53:51.466 GestureRec[1264:707] Before, frame: {{0, 124}, {320, 160}}, bounds: {{0, 0}, {320, 160}}
2012-07-02 14:53:51.473 GestureRec[1264:707] After, frame: {{-4.81771, 121.591}, {329.635, 164.818}}, bounds: {{0, 0}, {320, 160}}
2012-07-02 14:53:51.480 GestureRec[1264:707] scale: 1.074539, velocity: 1.889658
2012-07-02 14:53:51.484 GestureRec[1264:707] Before, frame: {{-4.81771, 121.591}, {329.635, 164.818}}, bounds: {{0, 0}, {320, 160}}
2012-07-02 14:53:51.494 GestureRec[1264:707] After, frame: {{-17.103, 115.449}, {354.206, 177.103}}, bounds: {{0, 0}, {320, 160}}
2012-07-02 14:53:51.499 GestureRec[1264:707] scale: 1.000000, velocity: 1.889658
2012-07-02 14:53:51.506 GestureRec[1264:707] Before, frame: {{-17.103, 115.449}, {354.206, 177.103}}, bounds: {{0, 0}, {320, 160}}
2012-07-02 14:53:51.510 GestureRec[1264:707] After, frame: {{-17.103, 115.449}, {354.206, 177.103}}, bounds: {{0, 0}, {320, 160}}

我误解了某些内容或博客中的作者是错误的吗?

1 个答案:

答案 0 :(得分:5)

我想我已经明白了:

博客是正确的,甚至根据Apple themselves

  

要翻译或缩放坐标系,可以更改视图   边界矩形...

然而,边界不会改变,因为矩形本身仍然与您报告的尺寸相同,只有坐标系已经缩放。你看:

  

更改边界矩形设置基本坐标系   由视图执行的所有绘图开始。

并且因为我们从未明确地更改边界,所以只有视图的相对于其超视图的帧会发生变化。

事实上,我可以在数学上证明界限永远不会改变!在这里,改为运行此示例:

NSLog(@"scale: %f, velocity: %f", recognizer.scale, recognizer.velocity);
    NSLog(@"Before, frame: %@, bounds: %@", NSStringFromCGRect(recognizer.view.frame), NSStringFromCGRect(recognizer.view.bounds));
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    NSLog(@"After, frame: %@, bounds: %@ transform:%@", NSStringFromCGRect(recognizer.view.frame), NSStringFromCGRect(recognizer.view.bounds), NSStringFromCGAffineTransform(recognizer.view.transform));

    recognizer.scale = 1;

现在,如果您注意到,NSStringFromCGAffineTransform()报告的值乘以边界等于视图的帧。但博客说的是什么呢?不必然准确是正确的。变换矩阵不仅可以应用于这个东西的x和y,如果我们真的想要,我们可以变换z值并旋转,翻转,并且每个方向,所有这些都以非线性方式改变帧属性,尤其是在黑手党中使用时。

一个有趣的难题,如果我自己这样说的话。