Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScrollView内部UILabel上的触摸检测_Ios_Objective C_Xcode_Cocoa Touch_Uilabel - Fatal编程技术网

Ios UIScrollView内部UILabel上的触摸检测

Ios UIScrollView内部UILabel上的触摸检测,ios,objective-c,xcode,cocoa-touch,uilabel,Ios,Objective C,Xcode,Cocoa Touch,Uilabel,我有一个UIScrollView,里面有UILabels。我需要检测ui标签的触摸事件。目前,它只检测到第二个标签内的触摸。它忽略了第一个问题 我有密码- 创建UIScrollView backGroundView = [[UIScrollView alloc] init]; backGroundView.frame= self.view.frame; backGroundView.userInteractionEnabled = YES; [backGroundView setScrollEn

我有一个
UIScrollView
,里面有
UILabels
。我需要检测
ui标签的触摸事件。目前,它只检测到第二个标签内的触摸。它忽略了第一个问题

我有密码-

创建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];
创建
ui标签

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");
    }
}

你从哪里开始写触摸

如果要检测标签中的触摸,必须创建标签的子类并将触摸写入其中开始检测触摸事件,您可以使用以下方法:

    UITapGestureRecognizer *labelTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapped)];
    labelTap.numberOfTapsRequired=1;
    [yourLabel addGestureRecognizer:labelTap];
在labelTapped方法内处理触碰事件:

-(void)labelTapped
{
  //your code to handle tap
}

UIScrollview
上未检测到触摸事件,以获取您的需求,并在标签上添加点击手势

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

  -(void)singleTapGestureCaptured{

        NSLog(@"touch detected");
}

您可以找到使用TapGestureRecograiser这样的

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...
}

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

这应该在注释中。>您在哪里编写了触摸开始?不,不是这样的。Ashish正在使用UITapGestureRecognitor。最初是touchesBega。阅读解决方案后,我切换到手势识别器。是否也可以传递UILabel的标签。我还将实现几个其他UILabel,因此我必须检测哪个标签是tapped@AshishAgarwal:是的,我刚刚检查了代码是否可行。我已用建议的更改更新了问题。此时它只检测到第二个手势,因为第一个标签被第二个标签覆盖。@AshishAgarwal:refere也可以传递UILabel的标签。我还将实现几个其他UILabel,因此我必须检测哪个标签被点击,我将它添加到所有标签中-
[labelone AddgestureRecognitor:singleFingerTap];[labeltwo AddGestureRecognitor:singleFingerTap]并且它只检测到我添加到的最后一个标签中的点击。忽略第一个。只需尝试此添加[self.view将Subview添加到前台:[(UITapgestureRecognitizer*)发件人视图];如果(recognizer.view.tag==1){。。。