CGAffineTransform更改view.bounds? [英] CGAffineTransform changes view.bounds?

查看:116
本文介绍了CGAffineTransform更改view.bounds?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图都有两个框架(超级视图的坐标系中的坐标) 和边界(自己坐标系中的坐标)属性,但是如果您 变换视图,您不应使用或依赖于frame属性 不再.如果使用转换,请使用边界 仅属性,而不是框架属性,因为应用了转换 到边界,但不一定准确地反映在框架中

Views have both a frame (coordinates in superview's coordinate system) and bounds (coordinates in own coordinate system) property, but if you transform a view, you should not use or rely on the frame property anymore. If you are using transformations, work with the bounds property only, not the frame property, as transformations are applied to the bounds, but aren't necessarily reflected accurately in frame

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

我想看看他在上段中的意思,并打印出框架"和界限"
而且我看到在紧缩过程中只有框架"在变化.

I wanted to see what he means in the above paragraph and printed 'frame' and 'bounds'
And I see only 'frame' is changing during the pinch.

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

我误解了吗,还是博客中的作者错了?

Am I misunderstanding something or the author in the blog is wrong?

推荐答案

我认为我已经掌握了:

该博客是正确的,甚至根据苹果本身:

The blog is correct, according even to Apple themselves:

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

To translate or scale the coordinate system, you alter the view's bounds rectangle...

但是边界不会改变,因为矩形本身仍然与您报告的矩形大小相同,只有坐标系随其缩放.您会看到:

However bounds doesn't change because the rectangle itself is still the same size you've reported it, only the coordinate system has scaled with it. You see:

更改边界矩形可使用以下命令设置基本坐标系 该视图执行的所有绘图都将开始.

Changing the bounds rectangle sets up the basic coordinate system with which all drawing performed by the view begins.

并且因为我们从不明确更改边界,所以仅相对于其父视图的视图框架会发生变化.

and because we never explicitly change bounds, only the view's frame relative to its superview changes.

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

In fact, I can mathematically prove the bounds never change! Here, run this sample instead:

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值并旋转,翻转,并且所有这些方式都以非线性方式改变帧属性,尤其是在配合使用时.

Now, if you notice, the values reported from NSStringFromCGAffineTransform() multiplied by the bounds is equivalent to the view's frame. But what about what the blog said? Not necessarily accurate is right. Transformation matrices can apply to more than just the x and y of this thing, if we really wanted to, we could transform z values and rotate, and flip, and every which way, all of these change the frame property in non-linear ways, especially when used in tandom.

一个有趣的难题,如果我自己说的话.

An interesting conundrum, if I do say so myself.

这篇关于CGAffineTransform更改view.bounds?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆