2

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.

4

4 回答 4

2

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

-(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];
}
于 2013-10-24T13:48:54.993 回答
1

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

于 2013-10-23T07:00:02.190 回答
1

这是错误:

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

写:

val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
[val1 addGestureRecognizer:tap1];
于 2013-10-23T07:05:35.007 回答
0

您在没有 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]
 }
于 2013-10-23T07:02:04.433 回答