1

如果我在 myView 中有很多子视图(所有视图都是 UIView 的实例)并且我设置了每个子视图的变换,可能是它的 setScale、旋转等。如何知道我设置的每个子视图的 xscale 和 yscale?

例如:

  subview1.transform = CGAffineTransformMakeScale(xscale1, yscale1);
  subview1.transform = CGAffineTransformRotate(subview1.transform, angle1);
  subview2.transform = CGAffineTransformMakeScale(xscale2, yscale2);
  subview2.transform = CGAffineTransformRotate(subview2.transform, angle2);
   .
   .
  subviewx.transform = CGAffineTransformMakeScale(xscalex, yscalex);
  subviewx.transform = CGAffineTransformRotate(subviewx.transform, angle3);

从示例用户在 subview.transform 中设置的内容,我需要在用户设置后知道 xscale 和 yscale。我认为这是要解决的一些方程式,请帮助我。^_^

谢谢

4

4 回答 4

2

从文档CGAffineTransform中记下该函数CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );。您可以像这样获得视图的比例

view.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(2.0, 0.5), CGAffineTransformMakeRotation(M_PI_4));
CGFloat xScale = view.transform.a / cosf(M_PI_4);
CGFloat yScale = view.transform.d  / cosf(M_PI_4);
于 2012-06-05T05:52:28.407 回答
1

您可以通过以下代码获得子视图的 x 和 y 比例:

for (UIView *subview in self.yourView.subviews)
{

        CGRect framelb1=subview.frame;
        NSNumber *num;
        NSNumber *ynum;
        num =[NSNumber numberWithInt:framelb1.origin.x];//x-value of subview
        ynum =[NSNumber numberWithInt:framelb1.origin.y];//y-value of subview
}
于 2012-06-05T05:10:39.530 回答
0

One approach is to apply the transform on CGRect and then compare with the original CGRect :

CGRect transformedBounds = CGRectApplyAffineTransform(self.bounds, self.transform);
CGFloat currScale = transformedBounds.size.width/ self.bounds.size.width;
于 2013-03-04T18:37:51.790 回答
0

you can get all subviews of view by -

NSArray *subViews = myView.subviews;

Now at the time of creation of your child views you can set tag property to them, and here on the basis o tag you can fetch individual view and perform task that you want.

于 2012-06-05T05:05:24.273 回答