Sea*_*lly 14 iphone events user-interface objective-c
我试图确定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];
[site release];
.
.
.    
然后我写回调.
 - (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];
  }
现在的问题是,无论我在视图中触摸的位置是打开的.如何确定是否仅触摸了我的标签?
Kev*_*tre 21
如果您将标签添加到课程中,您可以在触摸事件中对视图进行点击测试:
- (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] ...];
    ...
  }
}
另外,不要忘记释放您分配的URL(否则您将泄露).
Eug*_*ene 19
您可以在不覆盖touchesBegan的情况下执行此操作.使用手势识别器.
UILabel *label = ...;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)] autorelease];
[label addGestureRecognizer:recognizer];
- (void)tapAction {
  NSLog(@"tap performed");
}
| 归档时间: | 
 | 
| 查看次数: | 19086 次 | 
| 最近记录: |