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

似乎这只适用于 one UIView,这是添加的最后一个 换句话说UITapGestureRecognizer,它的视图是一对一的关系。这个对吗?我该如何解决?我必须UITapGestureRecog为每个单独创建一个吗?

4

4 回答 4

5

是的,只能UITapRecogniser一对一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. 首先我们用标签创建三个视图
  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 回答