tap gesture not added on navigation bar - iphone

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

Related

How to apply UITapGestureRecognizer on a specific label having some tag in objective c

I have 4 labels in a loop and i want to apply UITapGestureRecognizer on each label individually to perform some action how can i got this stuff if i want to apply gestures only on label with tag 2 any help please.
for(int i = 0 ; i < 4; i++){
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = LABEL_TAG+i;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Verdana-Bold" size:17.0];
label.text = #"someText";
[label sizeToFit];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gotTapped:)];
[self viewWithTag:LABEL_TAG+2 addGestureRecognizer:tap];
You only need to add UITapGestureRecognizer on each label.
for(UIlabel *label in cell.contentViews.subviews) {
if(label.tag == 1) {
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[tap setNumberOfTapsRequired:1];
[label addGestureRecognizer:tap];
} }

adding one gesture recognizer to a few UIView

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 to detect both main view UILongPressGestureRecognizer and subview UIPanGestureRecognizer

I encountered a problem with my UILongPressGestureRecognizer and UIPanGestureRecognizer.
i wish to long press in my self.view to add a view with UIPanGestureRecognizer in my self.view.
However the UIPanGestureRecognizer is not recognizing.
did i missed anything ?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initImagesAndGesture];
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.minimumPressDuration = 0.7;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[timeStamp setCenter:CGPointMake(tapLocation.x, tapLocation.y)];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75,115)];
drinkView.backgroundColor=[UIColor redColor];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[drinkView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
NSLog(#"UIGestureRecognizerStateChanged");
}
if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
NSLog(#"UIGestureRecognizerStateEnded");
}
}

detect a touch down event on UIImageView(s)

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..

iPhone:Can't tap UILabel at specified frame using UITapGestureRecognizer

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;