我可以在地图上检测用户是否已触摸除注释之外的任何位置?

时间:2012-11-15 01:24:36

标签: objective-c ios mkannotation

我正在构建一个应用程序,我想知道用户何时触摸了注释或地图上的任何其他位置。我有一个按钮,我想只在选择了注释时显示。因此,如果注释后的用户尝试触摸地图上的任何位置(如果不是另一个注释),则使按钮不可见。

目前,我尝试过touchesEnded方法,但问题是它无法识别注释和土地。

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    if([touches isMemberOfClass:[BuildingViewController class]])
        printf("Building");
    else
        printf("Land!");
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

touches是类NSSet的对象,因此您可以从NSSet获取对象并检查它的类成员身份。例如:

UITouch *touch = [touches anyObject];

你可以通过触摸获得UIView

UIView *touchedView = touch.view;

然后检查这个UIView类并将其与你的比较

[touchedView isMemberOfClass:[BuildingView class]]

另外,我建议你检查那个NSSet中的所有触摸。

BOOL isBuilding = NO;

for(UITouch *touch in touches){
    if([touch.view isMemberOfClass:[BuildingView class]]){
        isBuilding = YES;
        break;
    }
}

if(isBuilding){
    printf("Building");
}else{
    printf("Land!");
}
相关问题