我在视图中有一个UILabel控件。我想检测当这个标签被触摸时发生的触摸事件。我添加了以下代码,当触摸发生在视图上时,这些代码应该可以工作。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//应该只在触摸标签时调用,而不是一直调用..
[ UIApplication sharedApplication openURL:[NSURL URLWithString:@"http://www.mywebsite.com"] ];
}
每当特定的标签被触摸时,我的代码应该进一步执行这个过程,而不是当触摸发生在视图中的任何地方时。如何在touchesEnded函数下找出特定的标签(或)任何控件被触摸?
有人能在这方面给我指点一下吗?
谢谢。
发布于 2010-03-29 15:34:33
您可以通过在标签上执行hitTest来完成此操作:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:label];
if([label hitTest:point withEvent:nil]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"]];
} else {
[super touchesEnded:touches withEvent:event];
}
}https://stackoverflow.com/questions/2536311
复制相似问题