如何绘制 UIBezierPath 并与之交互

发布于 2024-11-16 07:54:25 字数 1659 浏览 2 评论 0原文

我想在我的应用程序中实现和设计建筑楼层的地图。在开始之前,我想得到一些建议。

我计划使用 UIBezierPath 来绘制形状。每个 UIBezierPath 将代表我的地图上的一家商店。 这是一个插图(map_with_UIBezierPath

我的代码结构如下:我有一个 UIViewController 和一个 UiView。在 UIViewController“viewDidLoad”方法中,我实例化 UIView,并在 UIView“drawRect”方法中,绘制如下形状(UIBezierPathExtension 继承自 UIBezierPath):

- (void)drawRect:(CGRect)rect {

context = UIGraphicsGetCurrentContext();

[[UIColor grayColor] setFill]; 
    [[UIColor greenColor] setStroke];

UIBezierPathExtension *aPath = [[UIBezierPathExtension alloc] init]; 
aPath.pathId = 1;
    [aPath moveToPoint:CGPointMake(227,34.25)];
[aPath addLineToPoint:CGPointMake(298.25,34.75)];
[aPath addLineToPoint:CGPointMake(298.5,82.5)];
[aPath addLineToPoint:CGPointMake(251,83)];
[aPath addLineToPoint:CGPointMake(251,67.5)];
[aPath addLineToPoint:CGPointMake(227.25,66.75)];   
    [aPath closePath]; 
aPath.lineWidth = 2;
[aPath fill]; 
[aPath stroke];
[paths addObject:aPath];

UIBezierPathExtension* aPath2 = [[UIBezierPathExtension alloc] init];
aPath2.pathId = 2;
[aPath2 moveToPoint:CGPointMake(251.25,90.5)];
[aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
[aPath2 addLineToPoint:CGPointMake(298.5,83)];
[aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
[aPath2 closePath];
aPath2.lineWidth = 2;
[aPath2 fill]; 
[aPath2 stroke];
[paths addObject:aPath2];   

    ...
}

我还在 UIViewController 中实现了平移和捏合手势。

现在,我问我如何与每一个形状进行交互。我想检测它的一次点击,改变他的颜色并显示一个像 那样的菜单 在选定的形状上。

有人可以告诉我正确的方向吗?

提前谢谢

I would like to implement and design a map for a building floor in my application. Before starting, I would like to have some advices.

I'm planning to use UIBezierPath to draw the shapes. Each UIBezierPath will represent a shop on my map.
Here is an illustration (map_with_UIBezierPath)

My code structure is the following: I have a UIViewController And a UiView. In the UIViewController "viewDidLoad" method, I instantiate the UIView and in the UIView "drawRect" method, I draw the shapes like following (UIBezierPathExtension inherit from UIBezierPath):

- (void)drawRect:(CGRect)rect {

context = UIGraphicsGetCurrentContext();

[[UIColor grayColor] setFill]; 
    [[UIColor greenColor] setStroke];

UIBezierPathExtension *aPath = [[UIBezierPathExtension alloc] init]; 
aPath.pathId = 1;
    [aPath moveToPoint:CGPointMake(227,34.25)];
[aPath addLineToPoint:CGPointMake(298.25,34.75)];
[aPath addLineToPoint:CGPointMake(298.5,82.5)];
[aPath addLineToPoint:CGPointMake(251,83)];
[aPath addLineToPoint:CGPointMake(251,67.5)];
[aPath addLineToPoint:CGPointMake(227.25,66.75)];   
    [aPath closePath]; 
aPath.lineWidth = 2;
[aPath fill]; 
[aPath stroke];
[paths addObject:aPath];

UIBezierPathExtension* aPath2 = [[UIBezierPathExtension alloc] init];
aPath2.pathId = 2;
[aPath2 moveToPoint:CGPointMake(251.25,90.5)];
[aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
[aPath2 addLineToPoint:CGPointMake(298.5,83)];
[aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
[aPath2 closePath];
aPath2.lineWidth = 2;
[aPath2 fill]; 
[aPath2 stroke];
[paths addObject:aPath2];   

    ...
}

I have also implemented the pan and pinch gesture in the UIViewController.

Now, I'm asking me how I can interact with every single shapes. I would like to detect a single tap on it, change his color and display a menu like that on the selected shape.

Can someone tell me the right direction to take?

Thx in advance

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

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

发布评论

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

评论(1

简单气质女生网名 2024-11-23 07:54:25

您需要在视图中查找触摸事件(TouchesBegan、TouchesMoved、TouchesEnded、TouchesCancelled)。当您触摸时,您可以询问它在您的视图中的位置。您可以使用此位置来测试该点是否位于您的任何路径内,如果是,请执行您的酷操作。

使用您的示例代码,这里可能是一个粗略的 TouchesBegan...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint pointTouched = [touch locationInView:self];
        for (UIBezierPath *path in paths) {
            if ([path containsPoint:point]) {
                // do something cool with your path
                // or more likely, set a flag to do the cool thing when drawing
            }
        }
    }
}

不要忘记您应该处理所有触摸事件并对每个事件做一些明智的事情。另外,上面的代码支持多点触摸,但您可能只想允许单点触摸,在这种情况下,有一些方法可以消除“触摸”循环。

You need to look for touch events (TouchesBegan, TouchesMoved, TouchesEnded, TouchesCancelled) in your view. When you get a touch, you can ask it for its location in your view. You can use this location to test whether that point is inside any of your paths, if it is, do your cool stuff.

Using your example code, here might be a rough TouchesBegan...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint pointTouched = [touch locationInView:self];
        for (UIBezierPath *path in paths) {
            if ([path containsPoint:point]) {
                // do something cool with your path
                // or more likely, set a flag to do the cool thing when drawing
            }
        }
    }
}

Don't forget that you should handle all the touch events and do something sensible with each. Also, the above code is multitouch capable, but you may want to only allow a single touch, in which case there are ways to eliminiate the "touches" loop.

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