【问题标题】:Deep copy UIView subviews to one UIView将 UIView 子视图深度复制到一个 UIView
【发布时间】:2014-04-24 11:10:09
【问题描述】:

我需要提取所有嵌套的子视图(从多个视图中)并添加它们,并将它们正确定位到一个 UIView 1 级深度。

NSArray*contentViews;    // views with complicated nesting of subViews
UIView *flatView;      // All the subviews but as simple subviews of the parent.

[self deepCopyAllSubviews:[contentViews[i++] subviews] to newView:flatView];

我的问题是如何找到与 flatView 相关的正确原点?

-(void) deepCopyAllSubviews:(NSArray*)subviews newView:flatView {

    [subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        CGRect frame = [(UIView*)obj frame];

        frame.origin.x =  ??

        [(UIView *)obj setFrame:frame];

        [flatView addSubview:obj];

        NSArray *subs = [(UIView*)obj subviews];

        if ([subs count]>0) {

            [self deepCopyAllSubviews:subs newView:flatView];
        }
    }];
}

【问题讨论】:

  • 我没有得到你的问题,但框架会给你 x 和 y 相对于它的父级的位置,其中 bound 将以绝对方式给出 x 和 y 位置。
  • 将子视图框架的原点添加到其父框架的原点以及其父框架的原点等等。

标签: ios objective-c xcode uiview


【解决方案1】:

您可以使用UIView 的转换方法。例如,在您的 ??? 行中,尝试:

frame.origin = [((UIView *)obj).superview convertPoint:frame.origin toView:flatView];

如果原始视图仍附加到其原始父视图,并且 flatView 存在于同一窗口的层次结构中,则这些转换方法将能够为您提供我认为您正在寻找的相对几何图形。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    相关资源
    最近更新 更多