I am used UITapGestureRecognizer in my apps but I can't to tap on the UILabel only so please help to solve this problem and I want to redirect on next view controller click on e letter of UILabel so please help me .......
self.label = [[UILabel alloc] initWithFrame:CGRectMake(45, 48, 94, 21)];
self.label.backgroundColor = [UIColor clearColor];
self.label.text = #"I like iPhone";
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self.label addGestureRecognizer:recognizer];
[self.view addSubview:label];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
Thanks in advance.
UILabel by default does not allow user interaction - you must explicitly enable it:
self.label.userInteractionEnabled = YES;
You have to enable user interaction for a label, it is disabled by default.
self.label.userInteractionEnabled = YES;
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];
}
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;
}
}
}
How can I get the UILongPressGestureRecognizer on uilabel.when i implement the following code it doesn't call the function. So please tell me what wrong i did ?
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LabelLongPressed:)];
longPress.minimumPressDuration = 0.5; // Seconds
longPress.numberOfTapsRequired = 0;
[objlblDuplicate addGestureRecognizer:longPress];
[longPress release];
By default UILabel is not able to get touch events.
objlblDuplicate.userInteractionEnabled = 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