如何确定UILabel是否被触动? [英] How can I determine if a UILabel was touched?
本文介绍了如何确定UILabel是否被触动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试确定UILabel是否被触动,如果是这样做的话。给... ..
。
。
。
UILabel * site = [[UILabel alloc] initWithFrame:CGRectMake(0,185,320,30)];
site.text = [retriever.plistDict valueForKey:@url];
site.textAlignment = UITextAlignmentCenter;
site.backgroundColor = [UIColor clearColor];
site.textColor = [UIColor whiteColor];
site.userInteractionEnabled = YES;
[theBgView addSubview:site];
[网站发布];
。
。
。
然后我写回调。
<$ p $ {code> - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
retriever = [PListRetriever sharedInstance];
CGPoint pt = [[touches anyObject] locationInView:self];
NSURL * target = [[NSURL alloc] initWithString:[retriever.plistDict valueForKey:@url]];
[[UIApplication sharedApplication] openURL:target];
}
问题是现在,无论我在哪里查看网址正在开放如何确定是否只有我的标签被触摸?
解决方案
如果您将标签添加到课程中,您可以触摸事件中您的视图中的点击测试:
- (void)touchesEnded:(NSSet *)触及Event :( UIEvent *)event
{
UITouch * touch = [[event allTouches] anyObject];
if(CGRectContainsPoint([self.site frame],[touch locationInView:self.view]))
{
NSURL * target = [[NSURL alloc] ...];
...
}
}
另外,不要忘记释放您分配的URL(否则您正在泄漏)。
I am trying to determine if a UILabel was touched and if so do something. Give ..
.
.
.
UILabel * site = [[UILabel alloc] initWithFrame:CGRectMake(0, 185, 320, 30)];
site.text = [retriever.plistDict valueForKey:@"url"];
site.textAlignment =UITextAlignmentCenter;
site.backgroundColor = [UIColor clearColor];
site.textColor = [UIColor whiteColor];
site.userInteractionEnabled = YES;
[theBgView addSubview:site];
[site release];
.
.
.
Then I write the callback.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
retriever = [PListRetriever sharedInstance];
CGPoint pt = [[touches anyObject] locationInView: self];
NSURL *target = [[NSURL alloc] initWithString:[retriever.plistDict valueForKey:@"url"]];
[[UIApplication sharedApplication] openURL:target];
}
The problem is right now, no matter where I touch in the View the Url is being opened. How do I determine if only just my label was touched?
解决方案
If you add the label to the class, you can do a hit-test on your view in the touch event with:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.site frame], [touch locationInView:self.view]))
{
NSURL *target = [[NSURL alloc] ...];
...
}
}
Also, don't forget to release the URL you allocate (otherwise you are leaking).
这篇关于如何确定UILabel是否被触动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文