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?
Yes, there can be only one UITapRecogniser for one UIView. You have to take different recognizers for different views although their action can be same.
Also see this link.
I think that you just have to add the gesture recognizer to the view that contains your storyImageView, storyTitleLabel, etc. as its subviews.
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];
You can add Same UITapGestureRecognizer to multiple View using this code.
Steps Are:
First we create three view with tag
Then we create NSMutableArray and add this view to array
After that add UITapGestureRecognizer to view
In UITapGestureRecognizer method we check the tag of view to differentiate which view tapped.
Here 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;
}
}
}
Related
bigLabel = [[UILabel alloc] init];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[tap setNumberOfTapsRequired:1];
[bigLabel addGestureRecognizer:tap];
bigLabel.backgroundColor=[UIColor clearColor];
bigLabel.text = _referenceObject.textForCell;
//bigLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"Header1.png"]];
bigLabel.font = [UIFont fontWithName:#"HelveticaNeueLTStd-Bd" size: 21.0];
bigLabel.font =[UIFont boldSystemFontOfSize:21.0f];
bigLabel.textColor = [UIColor whiteColor];
[bigLabel sizeToFit];
[self.navigationItem setTitleView:bigLabel];
Use the following code :
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
[navController.navigationBar addGestureRecognizer:tapRecon];
you need to set
bigLabel.userInteractionEnabled = YES;
because by default UILabel instances userInteractionEnabled is NO
Try setting a frame to it like so:
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hideKeyBoard)];
tapGesture.cancelsTouchesInView = NO;
[self.navigationController.view addGestureRecognizer:tapGesture];
-(void)hideKeyBoard
{
[self.view endEditing:YES];
}
I placed 4 UIImageView on UIView and I named them
UIImageView myImg1 = [[UIImageView alloc] init];
UIImageView myImg2 = [[UIImageView alloc] init];
UIImageView myImg3 = [[UIImageView alloc] init];
UIImageView myImg4 = [[UIImageView alloc] init];
How can I tell the compiler that I have just touched down UIImageView 1 or UIImaveView 2. If I only can use UIImageView to do this task, not buttons at all. Please guide me about this issue. Thanks
A stylish solution is to use the UIGestureRecognizer object:
in your loadView method (or anywhere you code the UIImageView(s) drawing...):
UITapGestureRecognizer *tapRecognizer;
UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[anImageView setTag:0]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
[anImageView setBackgroundColor:[UIColor redColor]];
[anImageView setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[anImageView addGestureRecognizer:tapRecognizer];
[contentView addSubview:anImageView];
[anImageView release];
UIImageView *anotherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 180, 100, 100)];
[anotherImageView setTag:1]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
[anotherImageView setBackgroundColor:[UIColor greenColor]];
[anotherImageView setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[anotherImageView addGestureRecognizer:tapRecognizer];
[contentView addSubview:anotherImageView];
[anotherImageView release];
This is a simple imageViewDidTapped: sample method that you can use to distinguish between the two sample UIImageView objects above:
- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;
UIImageView *tappedImageView = (UIImageView *)[tapGesture view];
switch (tappedImageView.tag) {
case 0:
NSLog(#"UIImageView 1 was tapped");
break;
case 1:
NSLog(#"UIImageView 2 was tapped");
break;
default:
break;
}
}
You should consider using the tag property for this. Give each image a unique tag, then use this information to determine which image was touched. For example, set the tags by
UIImageView myImg1 = [[UIImageView alloc] init];
myImg1.tag = 1;
and when you receive a touch for img, call
img.tag
to retrieve the tag.
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[myImg1 addGestureRecognizer:singleFingerTap];
[myImg2 addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
And action method
-(void)handleSingleTap:(UIGestureRecognizer*)sender{
//your code here
}
You can do like this.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if(CGRectContainsPoint(myImg1.view.frame,touchLocation))
{
NSLog(#" Image 1");
}
else if(CGRectCont..
Note..the NSLog will be outputted if the point lies in more than one image view..
in that case..check which image view is on top..
I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:#"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(#"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(#"%#", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:#"dog" withDuration:(12) ofType:#"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:#selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(#"%#",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.
I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:
Create backgroundView with the same frame as touchableView:
UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of backgroundView to clearColor:
backgroundView.backgroundColor = [UIColor clearColor];
Reset position of touchableView:
CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of touchableView:
touchableView.userInteractionEnabled = NO;
Add touchableView as subview to backgroundView:
[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to backgroundView.
Add backgroundView to view that you want.
Now you can hide touchableView but you will still receive gesture recognizers.
I don't test this but I think it should work.
sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview
I am initializing my gesture recognizers with the following code when I initialize a view. However, after multiple times of presenting a view on top of the one with the gesture recognizers and dismissing it with presentModalViewController and dismissModelViewController, the gesture recognizers stop working on the original view. I've tried manually releasing the recognizers in the view's dealloc function rather than using autorelease, but it doesn't seem to help. Does anyone have any ideas? Thanks!
Also, I should mention that this problem only happens on the device, and not the simulator.
-(void) initializeGestures {
recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)] autorelease];
//recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
swipeLeftGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureLeft:)] autorelease];
//swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureLeft:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.delegate = self;
swipeRightGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureRight:)] autorelease];
//swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureRight:)];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight; // default
[self.view addGestureRecognizer:swipeRightGesture];
swipeRightGesture.delegate = self;
}
-(IBAction) handleSwipeGestureLeft:(UISwipeGestureRecognizer*)sender
{
[self swipeLeft];
}
-(IBAction) handleSwipeGestureRight:(UISwipeGestureRecognizer*)sender
{
[self swipeRight];
}
-(IBAction) handleTapGesture:(UITapGestureRecognizer *) sender {
[self gotoDefinition];
}
did the view with gesture recognizers dealloc when you present another viewController?
this will cause the view remove gesture recognizers on it
Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.
As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.
Here's how I would do it:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.
[webView setUserInteractionEnabled:YES]