Action on UILabel in iphone - iphone

I have created some set of labels programmatically to display on the screen and i want after clicking on label some action should perform.
Please don't suggest me about UIButton. I want to do it for UILabel. After clicking on the label another detail view should appear.
Please help me to solve this problem without using Inteface Builder.

Finally i came up with the solution and i got the result
[titleLbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[titleLbl addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

make IBoutlet of your label,
implement touchesBegin in your controller, pull out the CGPoint - touchCoordinate, check
CGRectContainsPoint(label.frame,touchCoordinate)
{
//you got the touch action on your label
}

In
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
.......
.......
CGPoint touch;//Touch Location
if(CGRectContainsPoint([objectOfLable frame], [touch locationInView:self.view ]) )
{
Do What you Want
}
}
Try This

Related

UISearchBar doesn't respond to touch event

I am designing an IPhone app using MKMapView and UISearchBar, my issue is i need to make the keypad disappear when i touch the MapView or if i click the cancel button.
I tried using the codes:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[searchBar resignFirstResponder];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
But both dint help.
Here is the ScreenShot:
So please someone help me to resolve the issue. Thanks in advance.
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard {
[SearchBar resignFirstResponder];
}
(Where searchBar is the that is responsible for the keyboard)
Here is the updated one that doesn't use Xib file:
.h file:
.m file:
Hope this gives you an insight.

UIGestureRecognizer across entire view

I want to be able to make a gesture recognizer listening for a two finger swipe, anywhere on the screen.
Right now I have a UITableView in a sub-view that scrolls, of course, and it doesn't seem to pick up the UIGestureRecognizer that I set on the view. When you use the two fingers, it just scrolls...
Is there a way to make the entire view, maybe superview, listen for a two finger touch and when it recognizes it, it will instead of scrolling the table view, do what I want it to do?
EDIT: Heres my code but this is suposed to be a very general question. I just want to be able to have UIGestureRecognizers across the entire view, the whole thing.
navBarTitle.title = #"Crunch";
//opening the menuView from launch
//setup the customtable view as a subview in menuView
SDMenuViewController *mvc = [[[SDMenuViewController alloc] initWithNibName:#"SDNestedTableView" bundle:nil] autorelease];
[self addChildViewController:mvc];
[mvc didMoveToParentViewController:self];
[menuView addSubview:mvc.view];
[mvc.view setFrame:CGRectMake(mvc.view.frame.origin.x, mvc.view.frame.origin.y, mvc.view.frame.size.width, mvc.view.frame.size.height + 44)]; //add navBarHeight, for some strage reason
//add swipe down gesture on for the menu
UISwipeGestureRecognizer *swipeClose =[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(closeMenu)] autorelease];
swipeClose.numberOfTouchesRequired = 2;
swipeClose.direction = UISwipeGestureRecognizerDirectionUp;
UISwipeGestureRecognizer *swipeOpen = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(openMenu)] autorelease];
swipeOpen.numberOfTouchesRequired = 2;
swipeOpen.direction = UISwipeGestureRecognizerDirectionDown;
for (UIGestureRecognizer* rec in menuView.gestureRecognizers) {
[rec requireGestureRecognizerToFail:swipeClose];
[rec requireGestureRecognizerToFail:swipeOpen];
}
for (UIGestureRecognizer* rec in mvc.view.gestureRecognizers) {
[rec requireGestureRecognizerToFail:swipeClose];
[rec requireGestureRecognizerToFail:swipeOpen];
}
[mvc.view addGestureRecognizer:swipeClose];
[mvc.view addGestureRecognizer:swipeOpen];
[self.view addGestureRecognizer:swipeClose];
[self.view addGestureRecognizer:swipeOpen];
Maybe I did not explain my question clearly. But for what I was doing this ended up working, and maybe it will help other people in the future:
Two finger swipe in UIScrollview for iPad application
you can try to do something like this:
UISwipeGestureRecognizer* p = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(actionPan:)];
p.numberOfTouchesRequired = 2;
for (UIGestureRecognizer* rec in tableView.gestureRecognizers) {
[rec requireGestureRecognizerToFail:p];
}
[self.view addGestureRecognizer:p];
hope that will help
//In view did load
[self.view setMultipleTouchEnabled:YES];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForView:self.view] count] > 1) {
NSLog(#"%d active touches",[[event touchesForView:self.view] count]) ;
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
}
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
// do some thing...
}
}
i Hope this would solve your problem. and in touches enabled make the count equal to 2 or any number of touches.. :) feel free to ask if you have any doubts
  Add another view with frame of self.view over self.view like "viewForGesture" and add gesture recognizer on that view like
UIView *viewForGesture = [[ [UIView alloc]initWithFrame:self.view.frame] autorelease];
[viewForGesture setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:viewForGesture];
.
.
.
[viewForGesture addGestureRecognizer:swipeOpen];
[viewForGesture addGestureRecognizer:swipeClose];
There's two way of making UIView detecting touch. The approach you defined and want that's UIGestureRecognizer, that answer already given by Ezeki.
Another approach you can use to detect touch is to override UITouch delegates.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.tapCount==2)
{
//Do something ... (Here, you can also check for the object which touched.)
}
}

How to give action on click of an image

I am developing an app in which what i want is when a user clicks on an image in an image view
An Action should be fired(like when a button is clicked).
I don't want button image. I just want an image view and action on it.
Kindly help.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
-(void)tapDetected{
NSLog(#"single Tap on imageview");
}
According to your comment, if you want to know about image's index then you can do this by tag property of image view. Assign your image view's tag property as per your need and get the tag on Touch event.
For example :
Add UITapGestureRecognizer
ITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
Use its delegate method : - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
// called when touch is began or when user touches
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
int indexOfImageView = touch.view.tag;
}
Hope it helps you...

I can not touch the image when using UIScrollView

I use a UIScrollView to show a serial of UIImageView(the UIImageView is added as subview of scrollview), the problem is that I have rewrote the method in UIImageView:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
But when I click the image , the method is not called, it seems that the image did not receive the click event. What is the exact reason? How can I fix this?
(I have enable the exclusiveTouch of the UIImageView and the User Interavtion of all the view)
Set the UIImageView's exclusiveTouch and userInteractionEnabled properties to YES and try again. I find that subviews of scrollview's receive only some or no events, and setting exclusiveTouch allows the view to claim them immediately.
set
imageView.userInteractionEnabled = YES;
and if you just want a tap event for the imageview
just use the UITapGestureRecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[imageView addGestureRecognizer:tap];
then you must implement the method
- (void) tap:(UITapGestureRecognizer*)gesture
{
UIImageView *v = gesture.view;// that your ImageView tapped
}
imageView.userInteractionEnabled = NO;

Touch Up Inside for a UIImageView

I'm trying to implement touch event similar to a Touch Up Inside a UIButton. I've seen some codes using touchesBegan:withEvent but it looks I need to slide a bit my finger over the screen. I actually just want to touch in the image
Code:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(#"Image touched!");
}
Regards!
You could use the gesture to get the touch on UIImageView.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(ClickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:2];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
Implement the ClickEventOnImage function.
-(void) ClickEventOnImage:(id) sender
{
}
UIImageView is notorious regarding this as its userInteractionEnabled is set to NO by default. You will have to enable it by doing
imageView.userInteractionEnabled = YES;
And then you can use gesture recognizers or the normal touches* methods to handle touches.