Reputation: 8969
So I have the following code:
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];
It seems that this only works for one UIView
, which is the last one added In other words a UITapGestureRecognizer
and it's view is a one to one relationship. Is this correct? How do I fix this? Do I have to create a separate UITapGestureRecog
for each?
Upvotes: 2
Views: 8326
Reputation: 9143
You can add Same UITapGestureRecognizer
to multiple View using this code.
Steps Are:
NSMutableArray
and add this view to arrayHere is the code for the steps:
-(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;
}
}
}
Upvotes: -1
Reputation: 10251
try this,
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];
Upvotes: 0
Reputation: 108
I think that you just have to add the gesture recognizer to the view that contains your storyImageView
, storyTitleLabel
, etc. as its subviews.
Upvotes: 0