9

视图同时具有框架(父视图坐标系中的坐标)和边界(自身坐标系中的坐标)属性,但是如果您转换视图,则不应再使用或依赖框架属性。如果您正在使用转换,请仅使用 bounds 属性,而不是 frame 属性,因为转换应用于边界,但不一定准确反映在 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;
}

输出:(放大)

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

是我误解了什么还是博客中的作者错了?

4

1 回答 1

7

我想我明白了:

该博客是正确的,甚至根据苹果自己的说法:

要平移或缩放坐标系,您需要更改视图的边界矩形...

但是 bounds 并没有改变,因为矩形本身的大小仍然与您报告的大小相同,只有坐标系随它缩放。你看:

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

并且因为我们从不显式地改变边界,所以只有相对于其父视图的视图框架发生了变化。

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

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 值并旋转、翻转,以及每一种方式,所有这些都以非线性方式改变框架属性,尤其是串联使用时。

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

于 2012-07-02T06:06:58.177 回答