为 UIView 添加时,UITapGestureRecognizer 无法正常工作无法检测到问题

Posted

技术标签:

【中文标题】为 UIView 添加时,UITapGestureRecognizer 无法正常工作无法检测到问题【英文标题】:UITapGestureRecognizer not working when added for UIView Cannot detect the issue 【发布时间】:2013-10-23 06:53:01 【问题描述】:

UITapGestureRecognizer 在为 UIView 添加时不起作用。无法检测到问题。

注意:我没有在ViewDidLoad 中使用它。请帮我解决问题。提前致谢。

-(void)setSegmentValues:(NSArray *) values 

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(tapDetected:)];
    tap1.numberOfTapsRequired = 1;
    tap1.delegate=self;
    [val1 addGestureRecognizer:tap1];
    val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
    val1.backgroundColor = [UIColor magentaColor];
    label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
    label1.backgroundColor = [UIColor yellowColor];
    label1.text = [values objectAtIndex:0];
    label1.textAlignment = UITextAlignmentCenter;
    val1.userInteractionEnabled=YES;
    [val1 addGestureRecognizer:tap1];
    [val1 addSubview:label1];
    [self addSubview:val1];

其他东西:

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer

    NSLog(@"success1");
 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

    return YES;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

    return YES;

注意:我还添加了UIGestureRecognizerDelegate

【问题讨论】:

这是我的最终代码,它可以工作 【参考方案1】:

就这样做..你会得到正确的解决方案..

-(void)setSegmentValues:(NSArray *)values bgColor:(UIColor *)color
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedIndexValue:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;

        viewOne = [[UIView alloc]initWithFrame:CGRectMake(40, 50, 80, 60)];
        viewOne.backgroundColor = color;
        viewOne.tag = 0;
        UILabel *lblOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
        lblOne.text = [values objectAtIndex:0];
        lblOne.textAlignment = UITextAlignmentCenter;
        lblOne.backgroundColor = [UIColor clearColor];
        [viewOne addSubview:lblOne];

        viewOne.userInteractionEnabled = YES;
        [viewOne addGestureRecognizer:tap];
        [self addSubview:viewOne];
    

-(void)selectedIndexValue:(UIGestureRecognizer *)gesture

    int myViewTag = gesture.view.tag; 
    if (myViewTag == 0) 
    
        selectedIndex1 = 0;
        self.lblValue.text = [valueArray objectAtIndex:myViewTag];
        viewOne.backgroundColor = [UIColor colorWithRed:57.0f green:122.0f blue:255.0f alpha:1.0f];
        viewTwo.backgroundColor = viewColor;
        viewThree.backgroundColor = viewColor;
    
[self sendActionsForControlEvents:UIControlEventValueChanged];

【讨论】:

我为你取消了删除:)【参考方案2】:

你是否控制甚至进入“setSegmentValues:”方法? 确保您的方法是从 viewDidLoad 或 viewWillAppear 或任何 IBAction 调用的。

【讨论】:

是的,我的控件进入 setSegmentValues 方法。我不应该这样使用吗?无需从 viewDidLoad 或 viewWillAppear 调用? 你可以这样使用,但是你需要从某个地方调用它,这样方法才能被执行。 viewDidLoad & viewWillAppear & viewDidAppear 是生命周期方法,因此它们会自行执行。您可以从它们或任何 IBAction 调用您的方法,例如单击按钮。 只需在 viewDidLoad 中添加这个并尝试:[self setSegmentValues:abcArray];【参考方案3】:

这是错误:

[val1 addGestureRecognizer:tap1];
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];

写:

val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
[val1 addGestureRecognizer:tap1];

【讨论】:

可能这也是我犯的一个错误。但问题仍然存在。它不工作。感谢您的回复 是的,这是一个错误,谢谢。但这并不能解决 Gesture 不起作用的问题。【参考方案4】:

您在没有分配初始化的情况下向 UiView 添加了一个手势。 在添加手势之前初始化视图

请使用此代码

-(void)setSegmentValues:(NSArray *) values 
 
  val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
  UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self      action:@selector(tapDetected:)];
  tap1.numberOfTapsRequired = 1;
  tap1.delegate=self;
  [val1 addGestureRecognizer:tap1];

  val1.backgroundColor = [UIColor magentaColor];
  label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
   label1.backgroundColor = [UIColor yellowColor];
  label1.text = [values objectAtIndex:0];
  label1.textAlignment = UITextAlignmentCenter;
  val1.userInteractionEnabled=YES;
  [val1 addGestureRecognizer:tap1];
  [val1 addSubview:label1];
   [self addSubview:val1]
 

【讨论】:

尝试为标签禁用 userInteration

以上是关于为 UIView 添加时,UITapGestureRecognizer 无法正常工作无法检测到问题的主要内容,如果未能解决你的问题,请参考以下文章

UIGesture 返回 UIImageView 而不是自定义视图

iOS开发:Runtime解决UITapGesture重复点击问题

如何能够响应对导航项标题的点击?

将 UITapGesture 添加到 UIScrollView

视图中的 UITapGesture 时不调用 UIButton 操作

UITableview 标头中的 UITapGesture