如何剪切或禁用 UIView 的一部分?

发布于 2024-12-24 17:50:03 字数 1190 浏览 0 评论 0原文

我有一个 UIView 的自定义子类,LineDrawingView。我在主视图控制器中将其作为 @property 并将其添加到视图中。这工作得很好 - 它在当前视图之上创建一个透明视图,并使用 UIBezierPath、touchesBegan、touchesMoved 等,因此您可以通过拖动手指来在整个视图上绘制。

我需要将该绘图视图设为“L”形,这样我就可以在左下角有一个区域来放置各种控件。我想不出有什么办法可以使绘图视图成为“L”形,除了添加两个单独的矩形绘图视图之外,但这不起作用,因为当您将手指从一个矩形拖动到另一个矩形时,触摸会被中断。我提出的另一个解决方案是在绘图视图之上添加另一个视图。此视图应该阻止绘图,我还可以在其中找到我的控件,以便在启用绘图时它们仍然可用。

我尝试创建一个 UIView 并将其添加为绘图视图的子视图。我给它上了一个色调,这样我就可以检查它是否存在并且位于正确的位置。我预计这会阻止在新 UIView 的区域内进行绘制,但在 LineDrawingView 的整个区域内继续进行绘制。我还尝试在索引 2 处插入新的 UIView,并在索引 1 处插入 LineDrawingView。它仍然没有影响绘图。

    self.drawView = [[LineDrawingView alloc] initWithFrame:CGRectMake(0, 50, 768, 905)];
    [self.drawView setBackgroundColor:[UIColor clearColor]];

    // not effective in preventing drawing!!
    UIView *controlView = [[UIView alloc] initWithFrame:CGRectMake(0, 530, 310, 575)];
    [controlView setUserInteractionEnabled:NO];
    [controlView setBackgroundColor:[UIColor grayColor]];
    [controlView setAlpha:0.3];

    [self.view addSubview:drawView];
    [self.drawView addSubview:controlView];

我很想知道:我怎样才能...

创建一个“L”形绘图视图? 或者 剪下绘图视图的一部分,以便用户可以与其后面的内容进行交互? 或者 在绘图视图顶部强加一个区域,我可以在其中禁用绘图并添加控件?

I have a custom subclass of UIView, LineDrawingView. I have this as a @property in my main view controller and I add it to the view. This works fine - it creates a transparent view on top of the current view and uses UIBezierPath, touchesBegan, touchesMoved etc so you can draw all over the view by dragging your finger around.

I need to make that drawing view "L" shaped, so I can have an area in the bottom left corner where various controls are located. I can think of no way to make the drawing view "L" shaped, except maybe to add two separate rectangular drawing views, but this doesn't work because touches are interrupted when you drag your finger from one rectangle into the other. The other solution I've come up with is to add another view on top of the drawing view. This view should prevent drawing and I could also locate my controls within it so they are still usable while drawing is enabled.

I tried creating a UIView and adding it as a subview of the drawing view. I gave it a tint so I could check it was present and in the right place. I expected this to prevent drawing within the area of the new UIView, but drawing continues all over the area of the LineDrawingView. I also tried inserting the new UIView at index 2, with the LineDrawingView inserted at index 1. It still didn't affect the drawing.

    self.drawView = [[LineDrawingView alloc] initWithFrame:CGRectMake(0, 50, 768, 905)];
    [self.drawView setBackgroundColor:[UIColor clearColor]];

    // not effective in preventing drawing!!
    UIView *controlView = [[UIView alloc] initWithFrame:CGRectMake(0, 530, 310, 575)];
    [controlView setUserInteractionEnabled:NO];
    [controlView setBackgroundColor:[UIColor grayColor]];
    [controlView setAlpha:0.3];

    [self.view addSubview:drawView];
    [self.drawView addSubview:controlView];

I would love to know: how can I either...

Create an "L" shaped drawing view?
OR
Cut out a section of the drawing view so users can interact with what is behind it?
OR
Impose an area on top of the drawing view where I can disable drawing and add my controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心 2024-12-31 17:50:03

这里是创建透明圆角矩形 UIView 的教程 ,我认为你可以直接修改它,使其成为 L 形。

最重要的是要记住,您必须实现自己的 drawRect 并且我相信您自己的 hitTest 或触摸(例如 touchedBegan:withEvent:) 方法。

Here is a tutorial on creating a transparent rounded rectangle UIView, which I think you could modify in a straightforward manner to make it L shaped.

The most important thing to keep in mind that you'll have to implement your own drawRect and I believe also your own hitTest or touches (e.g. event handling like touchedBegan:withEvent:) methods.

少钕鈤記 2024-12-31 17:50:03

我就此事联系了苹果公司。事实证明,没有办法创建“L”形视图。解决方案是在绘图视图中测试触摸是否在禁止区域内,如果是,则阻止绘制线。我的控件已移动到一个单独的 UIView,当启用绘图时,该 UIView 会移动到前面。这意味着控件始终保持活动状态,并且绘图不能在控件区域内进行。

I contacted Apple about this. It turns out there is not a way to create an "L" shaped view. The solution was to, in the drawing view, test whether touches are within the forbidden area and, if so, prevent the line from being drawn. My controls have been moved to a separate UIView which is moved to the front when drawing is enabled. This means the controls remain active the whole time and drawing cannot take place within the area of the controls.

空心↖ 2024-12-31 17:50:03

如果你想绘制一个 L 形绘图视图,你可以通过仅连接两条线来完成此操作.. 就像我创建了一个箭头一样.. 如果你想在移动(拖动)任何对象时停止绘图.. 你只需需要在您的触摸移动方法中应用检查(例如移动,)..

这是绘制箭头的代码(您可以使用它作为参考来绘制任何类型的形状):如何使用 Core Graphics 绘制箭头?

代码检查您是否已击中路径(意味着您的触摸点位于贝塞尔路径中)

if([[self tapTargetForPath:((UIBezierPath *)[testDict objectForKey:@"path"])] containsPoint:startPoint])// if starting touch is in bezierpath
        {
                    ishitInPath = YES;
                    isMoving = YES;
}


// to easily detect (or select a bezier path object)
- (UIBezierPath *)tapTargetForPath:(UIBezierPath *)path
{
    if (path == nil) {
        return nil;
    }

    CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(35.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
    if (tapTargetPath == NULL) {
        return nil;
    }

    UIBezierPath *tapTarget = [UIBezierPath bezierPathWithCGPath:tapTargetPath];
    CGPathRelease(tapTargetPath);
    return tapTarget;
}

if you want to draw an L shape drawing view you can do this by concatenating two lines only .. just like i have created an arrow .. and if you want to stop drawing while you are moving (dragging ) any object .. you just need to apply a check in your touches moved method (like is moved , )..

here is the code to draw an arrow (you can use this as a reference to draw any kind of shape) : How can I draw an arrow using Core Graphics?

code to check if you have hit the path (means your touch point is in the bezier path )

if([[self tapTargetForPath:((UIBezierPath *)[testDict objectForKey:@"path"])] containsPoint:startPoint])// if starting touch is in bezierpath
        {
                    ishitInPath = YES;
                    isMoving = YES;
}


// to easily detect (or select a bezier path object)
- (UIBezierPath *)tapTargetForPath:(UIBezierPath *)path
{
    if (path == nil) {
        return nil;
    }

    CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(35.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
    if (tapTargetPath == NULL) {
        return nil;
    }

    UIBezierPath *tapTarget = [UIBezierPath bezierPathWithCGPath:tapTargetPath];
    CGPathRelease(tapTargetPath);
    return tapTarget;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文