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

本文是小编为大家收集整理的关于CGAffineTransform改变view.bounds?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

我是误解了博客中的某些东西还是作者是错误的?

推荐答案

我想我已经知道了:

博客是正确的,即使是数学上证明界限永远不会改变!在这里,请运行此示例:

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

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

本文地址:https://www.itbaoku.cn/post/2091137.html

问题描述

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

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

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

推荐答案

I think I've got it:

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;

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.