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

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 に答える
1

「setSegmentValues:」メソッドでも制御しますか? メソッドが viewDidLoad または viewWillAppear または任意の IBAction から呼び出されていることを確認してください。

于 2013-10-23T07:00:02.190 に答える
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 に答える