简体   繁体   English

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

[英]UITapGestureRecognizer not working when added for UIView Cannot detect the issue

UITapGestureRecognizer not working when added for UIView . UIView添加UITapGestureRecognizer无效。 Cannot detect the issue. 无法检测到问题。

Note: I am not using it in ViewDidLoad . 注意:我不在ViewDidLoad使用它。 Please help me is solving the problem. 请帮助我解决问题。 Thanks in Advance. 提前致谢。

-(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];
}

Other stuffs: 其他东西:

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
    NSLog(@"success1");
} 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Note: I've also added UIGestureRecognizerDelegate . 注意:我还添加了UIGestureRecognizerDelegate

Just do like this.. You will get correct solution.. 就是这样..您将获得正确的解决方案..

-(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];
}

Is you control even going in "setSegmentValues:" method? 您是否甚至控制“ setSegmentValues:”方法? Make sure your method is being called from viewDidLoad or viewWillAppear or any IBAction. 确保从viewDidLoad或viewWillAppear或任何IBAction中调用您的方法。

this is error: 这是错误的:

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

write: 写:

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

You added a gesture to an UiView without alloc init. 您将手势添加到了没有alloc init的UiView。 Initialize the View before adding Gesture to it 在向其添加手势之前初始化视图

Please use this code 请使用此代码

-(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]
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM