2

だから私は次のコードを持っています:

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

これは、最後に追加された1つに対してのみ機能するようですUIView。言い換えると、aUITapGestureRecognizerとそのビューは1対1の関係です。これは正しいです?これを修正するにはどうすればよいですか?それぞれに個別に作成する必要がUITapGestureRecogありますか?

4

4 に答える 4

5

UITapRecogniserはい、1つに1つしか存在できませんUIView。それらのアクションは同じである可能性がありますが、異なるビューに対して異なるレコグナイザーを使用する必要があります。このリンク
も参照してください。

于 2012-07-19T06:06:58.080 に答える
0

これを試して、

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];

[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];
于 2012-07-19T06:02:49.420 に答える
0

storyImageViewサブビューとして、、storyTitleLabelなどを含むビューにジェスチャレコグナイザーを追加するだけでよいと思います。

于 2012-07-19T06:07:03.477 に答える
-1

UITapGestureRecognizerこのコードを使用して、複数のビューに同じものを追加できます。

手順は次のとおりです。

  1. まず、タグ付きの3つのビューを作成します
  2. 次にNSMutableArray、このビューを作成して配列に追加します
  3. その後、表示するUITapGestureRecognizerを追加します
  4. UITapGestureRecognizerメソッドでは、ビューのタグをチェックして、タップされたビューを区別します。

手順のコードは次のとおりです。

-(Void)viewDidLoad {
    [super viewDidLoad];

    //First create three View
    UIView  *view1 = [[UIView alloc] initWithFrame: CGRectMake (5 , 171, 152, 152)];    
    view1.tag = 1;    //add tag to view
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view1];

    UIView  *  view2 = [[UIView alloc] initWithFrame: CGRectMake ( 163, 171,  152, 152)];   
    view2.tag = 2;   //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    UIView * view3 = [[UIView alloc] initWithFrame: CGRectMake ( 5, 330,  152, 152)];    
    view2.tag = 3;    //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    //Now create mutable array to hold our view
    NSMutableArray * ary=[[NSMutableArray alloc] init];
    [ary addObject:view1];
    [ary addObject:view2];
    [ary addObject:view3];


    //now we add tap gesture to view
    for (UIView *view in ary) {
        UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(answerDoubleTapped:)];
        answerDoubleTapGesture.numberOfTapsRequired = 2;
        [answer4View addGestureRecognizer:answerDoubleTapGesture];
    }
   }

-(void)answerDoubleTapped:(UITapGestureRecognizer *)recognizer {
    //Check which view is tapped
    switch (recognizer.view.tag) {
        case 1:
        {
            NSLog(@"First View Tapped");
            break;
        }case 2:
        {
            NSLog(@"Second View Tapped");
            break;
        }case 3:
        {
            NSLog(@"Third View Tapped");
            break;
        }case 4:
        {
            NSLog(@"Forth View Tapped");
            break;
        }default:
        {
            break;
        }
    }
}
于 2013-07-31T10:36:38.960 に答える