如何解决如何绘制和与UIBezierPath交互
| 我想在我的应用程序中实现和设计建筑物地板的地图。在开始之前,我想提出一些建议。 我打算使用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中的平移和收缩手势。
现在,我问我如何与每个形状互动。我想检测一下它的点击状态,更改他的颜色,并在所选形状上显示类似的菜单。
有人可以告诉我正确的方向吗?
提前Thx
解决方法
您需要在视图中查找触摸事件(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
}
}
}
}
不要忘记,您应该处理所有触摸事件,并对每个事件进行明智的处理。同样,上面的代码具有多点触摸功能,但是您可能只希望单点触摸,在这种情况下,有多种方法可以消除“ touches \”循环。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。