編輯以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