本文介绍了为交叉的UIViews(ObjectiveC)绘制一个共同的轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有 UIView 。在这个 UIView 里面,我有两个相交的子视图。我想在这两个子 UIViews 之间画一个共同的轮廓。我如何得到这个?I have a UIView. Inside this UIView, I have two intersecting subviews. I want to draw a common outline between these two sub-UIViews. How do I get about this?说清楚:[mainView addSubview:subView1];[mainView addSubview:subView2];我需要为这些相交的 UIView s。I need to draw an outline for both these intersecting UIViews.推荐答案您可以组合两个矩形的形状并从最终形状创建新的子图层。这一层将作为你的大纲。You can combine shapes of both rectangles and create new sublayer from the final shape. This layer will act as your outline.查看我的代码,完全注释:See my code, its fully commented:// Create shape of merged rectanglesUIBezierPath *outlinePath = [UIBezierPath bezierPathWithRect:subView1.frame];[outlinePath appendPath:[UIBezierPath bezierPathWithRect:subView2.frame]];// Configure the outlineUIColor *outlineColor = [UIColor redColor];CGFloat outlineWidth = 1.0f * [[UIScreen mainScreen] scale];// Create shape layer representing the outline shapeCAShapeLayer *outlineLayer = [CAShapeLayer layer];outlineLayer.frame = mainView.bounds;outlineLayer.fillColor = outlineColor.CGColor;outlineLayer.strokeColor = outlineColor.CGColor;outlineLayer.lineWidth = outlineWidth;// Set the path to the layeroutlineLayer.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];输出如下: 编辑:我最初误解了这个问题。下面是我的原始答案,其中概述了交叉点。Edit: I misunderstood the question at first. Below is my original answer which outlines rects intersection.您可以创建另一个将代表交叉点的视图,并将其添加到两个子视图上方。它的框架将是 subView1 和 subView2 框架的交叉点秒。只需使用图层属性明确背景颜色和边框You can just create another view which will be representing the intersection and add it above the two subviews. It's frame would be intersection of the subView1 and subView2 frames. Just give it clear background color and border using its layer property您的代码可能如下所示:Your code could look like this:// Calculate intersection of 2 viewsCGRect intersectionRect = CGRectIntersection(subView1.frame, subView2.frame);// Create intersection viewUIView *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; 这篇关于为交叉的UIViews(ObjectiveC)绘制一个共同的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!