编辑以IMPROVE数据模型
要做到这一点,你将不得不与背景的地图图像一个UIView。您可以使用UIImageView或通过将图像自己渲染到drawRect中来完成此操作。
然后,您将定义几个CGPath引用。每个建筑物通过做这样的事情...... How to create CGPathRef from Array of points这些点将成为每个建筑物的角落。
现在将这些路径以某种方式存储在数组中。您需要为每个“可点击”建筑物建造一条路径。
我现在的路径存储大厦对象或东西在里面......
@interface Building : NSObject
@property (nonatomic) CGPath path;
@end
在UIView子类重写- (void)touchesBegan...
。然后,您可以获取触摸点并遍历您的路径,找到哪个被触摸...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
for (Building *building in self.buildings) {
if (CGPathContainsPoint(building.path, CGAffineTransformIdentity, touchPoint, YES)) {
//the touch was inside this building!
//now do something with this knowledge.
}
}
}
所以你说把我现在的UIImageView内一个UIView,然后只检查所有触摸的位置? – BloonsTowerDefence 2013-02-12 14:16:54
对不起,没有很好解释。将编辑。\ – Fogmeister 2013-02-12 14:17:35