【问题标题】:How to get frame of subview after apply transform on mainView?在 mainView 上应用变换后如何获取子视图的框架?
【发布时间】:2013-04-29 20:25:27
【问题描述】:

我创建了UIView 的mainView 对象并在其上添加了一个子视图。我在 mainView 上应用了变换以减小帧大小。但是 mainView 的子视图框架没有减少。如何减小这个子视图的大小。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CGFloat widthM=1200.0;
    CGFloat heightM=1800.0;
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthM, heightM)];
    mainView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"te.png"]];
    [self.view addSubview:mainView];
    CGFloat yourDesiredWidth = 250.0;
    CGFloat yourDesiredHeight = yourDesiredWidth *heightM/widthM;
    CGAffineTransform scalingTransform;
    scalingTransform = CGAffineTransformMakeScale(yourDesiredWidth/mainView.frame.size.width, yourDesiredHeight/mainView.frame.size.height);
     mainView.transform = scalingTransform;
    mainView.center = self.view.center;
    NSLog(@"mainView:%@",mainView);
    UIView *subMainView= [[UIView alloc] initWithFrame:CGRectMake(100, 100, 1000, 1200)];
    subMainView.backgroundColor = [UIColor redColor];
    [mainView addSubview:subMainView];
    NSLog(@"subMainView:%@",subMainView);

}

这些视图的 NSlog:

mainView:<UIView: 0x8878490; frame = (35 62.5; 250 375); transform = [0.208333, 0, 0, 0.208333, 0, 0]; layer = <CALayer: 0x8879140>>
subMainView:<UIView: 0x887b8c0; frame = (100 100; 1000 1200); layer = <CALayer: 0x887c160>>

这里主视图的宽度是250,子视图的宽度是1000。但是当我在模拟器中得到输出时,子视图被正确占用,但它没有跨越主视图。怎么可能?变换后如何获取相对于主视图框架的子视图框架?

【问题讨论】:

    标签: ios uiview transform


    【解决方案1】:

    您看到的是预期行为。 UIView 的框架是相对于其父级的,因此当您对其父视图应用转换时它不会改变。虽然视图也会出现“扭曲”,但框架不会反映更改,因为它相对于其父级仍处于完全相同的位置。
    但是,我假设您希望获得相对于最顶部 UIView 的视图框架。在这种情况下,UIKit 提供了这些功能:

    • – [UIView convertPoint:toView:]
    • – [UIView convertPoint:fromView:]
    • – [UIView convertRect:toView:]
    • – [UIView convertRect:fromView:]

    我将这些应用于您的示例:

    CGRect frame = [[self view] convertRect:[subMainView frame] fromView:mainView];
    NSLog(@"subMainView:%@", NSStringFromCGRect(frame));
    

    这是输出:

    subMainView:{{55.8333, 83.3333}, {208.333, 250}}
    

    【讨论】:

      【解决方案2】:

      除了 s1m0n 答案之外,将变换矩阵应用于视图的美妙之处在于,您可以根据其原始坐标系进行推理(在您的情况下,您可以使用未变换的坐标系处理 subMainView ,这就是为什么即使 subMainView 的框架比 mainView 的转换后的框架大,它仍然不会越过父视图,因为它会自动转换)。这意味着当您有一个转换的父视图(例如旋转和缩放)并且您想要在相对于该父视图的特定点添加子视图时,您不必首先跟踪先前的转换以这样做。

      如果您真的有兴趣根据变换后的坐标系了解子视图的框架,那么将相同的变换应用于子视图的矩形就足够了:

      CGRect transformedFrame = CGRectApplyAffineTransform(subMainView.frame, mainView.transform);
      

      如果你然后NSLog这个CGRect,你会得到:

      Transformed frame: {{20.8333, 20.8333}, {208.333, 250}}
      

      我相信,这就是您正在寻找的价值观。我希望这能回答你的问题!

      【讨论】:

      • 在给出这个答案的时候,iOS 7 刚刚发布,更不用说 iOS 8。你接下来要做什么?否决一个 java 问题的答案,评论它在 C# 中不起作用? :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多