簡體   English   中英

為UIView添加UITapGestureRecognizer無法正常工作無法檢測到問題

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

UIView添加UITapGestureRecognizer無效。 無法檢測到問題。

注意:我不在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

就是這樣..您將獲得正確的解決方案..

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

您是否甚至控制“ setSegmentValues:”方法? 確保從viewDidLoad或viewWillAppear或任何IBAction中調用您的方法。

這是錯誤的:

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

寫:

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

您將手勢添加到了沒有alloc init的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]
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM