Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为相交的UIView(Objective)绘制公共轮廓?_Objective C_Ipad_Uiview_Border - Fatal编程技术网

Objective c 为相交的UIView(Objective)绘制公共轮廓?

Objective c 为相交的UIView(Objective)绘制公共轮廓?,objective-c,ipad,uiview,border,Objective C,Ipad,Uiview,Border,我有一个ui视图。在这个ui视图中,我有两个相交的子视图。我想在这两个子视图之间画一个共同的轮廓。我该怎么办 为了说明这一点: [mainView addSubview:subView1]; [mainView addSubview:subView2]; 我需要为这两个相交的UIViews绘制一个轮廓。您可以组合两个矩形的形状,并从最终形状创建新的子层。这一层将作为您的轮廓 请参阅我的代码,其完整注释如下: // Create shape of merged rectangles UIBez

我有一个
ui视图
。在这个
ui视图中,我有两个相交的子视图。我想在这两个子视图之间画一个共同的轮廓。我该怎么办

为了说明这一点:

[mainView addSubview:subView1];
[mainView addSubview:subView2];

我需要为这两个相交的
UIView
s绘制一个轮廓。

您可以组合两个矩形的形状,并从最终形状创建新的子层。这一层将作为您的轮廓

请参阅我的代码,其完整注释如下:

//  Create shape of merged rectangles
UIBezierPath *outlinePath = [UIBezierPath bezierPathWithRect:subView1.frame];
[outlinePath appendPath:[UIBezierPath bezierPathWithRect:subView2.frame]];

//  Configure the outline
UIColor *outlineColor = [UIColor redColor];
CGFloat outlineWidth = 1.0f * [[UIScreen mainScreen] scale];

//  Create shape layer representing the outline shape
CAShapeLayer *outlineLayer = [CAShapeLayer layer];
outlineLayer.frame = mainView.bounds;
outlineLayer.fillColor = outlineColor.CGColor;
outlineLayer.strokeColor = outlineColor.CGColor;
outlineLayer.lineWidth = outlineWidth;

//  Set the path to the layer
outlineLayer.path = outlinePath.CGPath;

//  Add sublayer at index 0 - below everything already in the view
//  so it looks like the border
//  "outlineLayer" is in fact the shape of the combined rectangles
//  outset by layer border
//  Moving it at the bottom of layer hierarchy imitates the border
[mainView.layer insertSublayer:outlineLayer atIndex:0];
输出如下所示:

//  Calculate intersection of 2 views
CGRect intersectionRect = CGRectIntersection(subView1.frame, subView2.frame);

//  Create intersection view
UIView *intersectionView = [[UIView alloc] initWithFrame:intersectionRect];
[mainView addSubview:intersectionView];

//  Configure intersection view (no background color, only border)
intersectionView.backgroundColor = [UIColor clearColor];
intersectionView.layer.borderColor = [UIColor redColor].CGColor;
intersectionView.layer.borderWidth = 1.0f;

编辑:起初我误解了这个问题。下面是我的原始答案,它概括了矩形交点

您只需创建另一个表示交叉点的视图,并将其添加到两个子视图上方。它的框架是子视图1和子视图2的交叉点。只需使用其
属性为其提供清晰的背景色和边框即可

您的代码可能如下所示:

//  Calculate intersection of 2 views
CGRect intersectionRect = CGRectIntersection(subView1.frame, subView2.frame);

//  Create intersection view
UIView *intersectionView = [[UIView alloc] initWithFrame:intersectionRect];
[mainView addSubview:intersectionView];

//  Configure intersection view (no background color, only border)
intersectionView.backgroundColor = [UIColor clearColor];
intersectionView.layer.borderColor = [UIColor redColor].CGColor;
intersectionView.layer.borderWidth = 1.0f;

没有简单的内置选项来归档此文件。我可以想出多种方法来画一个共同的轮廓。例如,可以在子视图下方添加两个视图,其中矩形延伸原始视图,以便可以看到轮廓。我很抱歉,但我真正想要的是共同的大纲。图中的两个视图是相交的,但红线不是一个共同的轮廓。对不起,一开始我误解了你的问题。看到我编辑过的答案,希望对你有帮助谢谢。这当然有帮助,不客气。如果这是你想要的,你可以考虑接受我的回答作为一个小小的回报: