1

我有一个UIScrollView,在这个里面我有UILabels。我需要检测UILabels. 目前,它只检测第二个标签内的触摸。它忽略了第一个。

我有代码-

创建 UIScrollView

backGroundView = [[UIScrollView alloc] init];
backGroundView.frame= self.view.frame;
backGroundView.userInteractionEnabled = YES;
[backGroundView setScrollEnabled:YES];
backGroundView.showsVerticalScrollIndicator = YES;
backGroundView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
backGroundView.delegate = self;

[self.view addSubview:backGroundView];

创建UILabel

UILabel *OneDay = [[UILabel alloc] initWithFrame:CGRectMake(15, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
OneDay.text = @"1d";
OneDay.tag = 1;
OneDay.userInteractionEnabled = YES;
OneDay.layer.borderColor = [UIColor grayColor].CGColor;
OneDay.layer.borderWidth = 1.0f;
OneDay.textAlignment = UITextAlignmentCenter;
[OneDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:OneDay];

UILabel *FiveDay = [[UILabel alloc] initWithFrame:CGRectMake(45, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
FiveDay.text = @"5d";
FiveDay.tag = 2;
FiveDay.userInteractionEnabled = YES;
FiveDay.layer.borderColor = [UIColor grayColor].CGColor;
FiveDay.layer.borderWidth = 1.0f;
FiveDay.textAlignment = UITextAlignmentCenter;
[FiveDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:FiveDay];

创建手势识别器

    UITapGestureRecognizer *detectTimeFrameChange = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeFrameLabelTapped:)];
detectTimeFrameChange.numberOfTapsRequired = 1;
[backGroundView addGestureRecognizer:detectTimeFrameChange];

处理手势

-(void)timeFrameLabelTapped:(UITapGestureRecognizer*)recognizer{
    if (recognizer.view.tag == 1) {
        NSLog(@"One pressed");
    }
    else if (recognizer.view.tag == 2){
        NSLog(@"2 pressed");
    }
}
4

5 回答 5

3

你可以使用这个:

    UITapGestureRecognizer *labelTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapped)];
    labelTap.numberOfTapsRequired=1;
    [yourLabel addGestureRecognizer:labelTap];

处理 labelTapped 方法中的触摸点击事件:

-(void)labelTapped
{
  //your code to handle tap
}
于 2013-04-19T06:33:17.797 回答
0

你在哪里写了touchesBegan?

如果要检测标签中的触摸,则必须创建标签的子类并在此处编写 touchesBegan 以检测触摸事件

于 2013-04-19T06:33:12.133 回答
0

未检测到触摸事件UIScrollview以获取您的要求,将点击手势添加到您的标签。

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured)];
        [OneDay addGestureRecognizer:singleTap]; 

  -(void)singleTapGestureCaptured{

        NSLog(@"touch detected");
}
于 2013-04-19T06:33:27.420 回答
0

这里的问题是您试图对多个视图使用相同的手势识别器。手势识别器一次只能附加到单个视图。您只接收来自最后一个视图的事件,因为这是识别器当前附加到的视图。要解决此问题,只需为要检测触摸的每个视图创建一个手势识别器。

于 2013-05-25T15:29:41.803 回答
0

你可以找到像这样使用 Tapgesturerecognizer ......

UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[self.scrollview addGestureRecognizer:singleFingerTap];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    if(recognizer.view.tag == 1){}
    //Do stuff here...
}
于 2013-04-19T06:33:54.260 回答