iOS UITaprecognizer not responding - ios

I am trying to add a tap recognizer to a UILabel programmatically. It is not working... I have searched and tried a millions things of stackoverflow, but I can't get it to work...
Here is my current code:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(more:)];
[self.moreLabel setUserInteractionEnabled:YES];
[self.moreLabel addGestureRecognizer:tap];
- (void)more:(UITapGestureRecognizer *)sender {
NSLog(#"HIT?");
}
It will work if I add it to the top view but I don't want that ;) Any help is appreciated.
Solved using #Savitha comment:
Summary of answer: "The view I was trying to touch wasn't under another view so either using bringToFront or setting it correctly in IB was the answere."

Bring moreLabel to front.
[self.view bringViewToFront:self.moreLabel];

Related

UITapGestureRecognizer in uipageviewcontroller

I have one question about UITapGestureRecognizer in UIPageViewController
I have a UILabel labe1 in uiviewcontroller vc1 ,and vc1 is embedded in UIPageViewController.
Now I wanna to get the single tap event of label, but when in add the follow code in vc1 viewDidLoad function it doesn't work.
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleLabel1Tap)];
[_label addGestureRecognizer:singleFingerTap];
But if change the touch focus to self.view, it works well.
Anyone could give me some suggestion, It will be very appreciated.
Perhaps the userinteraction property is not enabled, try this:
[_label setUserInteractionEnabled:YES];
Or change the color of the _label and parent view to make sure that the _label resides inside the parent view to successfully receive and handle touch events.

iOS SWRevealViewController how to hide the left part programatically

I included SWRevealViewController objective-c library with my swift project,
it's working fine,
my question is that i want to hide that left menu programtically when the user clicks on an empty space inside its table view.
i didn't know how, could you help please
If you want to hide the left menu programmatically just call the method
- (IBAction)revealToggle:(id)sender;
which is in SWRevealViewController.m class
If you want to capture the click on tableView's area where there are no cells, you can follow SunburstEnzo's advice.
Add a UITapUITapGestureRecognizer on the viewDidLoad method of the class with the tableView.
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tableviewTapped)];
tapGesture.numberOfTapsRequired = 1;
[tapGesture setCancelsTouchesInView:NO]; //really important
[self.tableView addGestureRecognizer:tapGesture];
- (void) tapped
{
[self.revealViewController revealToggle:nil];
}

disable swipe gesture for iCarousel

I am using iCarousel to display an array of images and I want to disable the swipe gesture. I did not find that in the documentation. not sure if this is doable or not
If you want to disable the swipe gesture then I think are you want to do something like programatically change the image.
For very simply disable the user interaction of carousel.
If you using storyboard then simple remove checkmark of User Inreaction Enabled
If you use by code then following code to disable the User Inreaction Enabled
yourcarousel.userInteractionEnabled = FALSE;
May this help lot to solve your problem.
#Junchao GU If you are Using
https://github.com/nicklockwood/iCarousel
They are using Tap gesture and pan gesture
You have to Comment
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
panGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:panGesture];
//add tap gesture recogniser
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTap:)];
tapGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:tapGesture];
in iCarousel.m File
I hope this will help you
It's bad idea to change source code of iCarousel. I think it's better to do next:
carouselView.contentView.gestureRecognizers?.removeAll()
Hope it helps to someone

Open an other ViewController by touching a UILabel

I have a ViewController with full of names, basically a contact list. When you touch one of the names, you will get a detail screen with informations: name, organization, building, phone number, email, etc. It uses a navigation controller, so you can go back to the phonebook.
I have a custom map in my application (basically a picture, with several layers and with a pin, that show you where you are on the picture). I would like to achieve that when someone touch the building UILabel it goes my MapViewController with some parameters. It's a simple UILabel, but contains useful information, something like 15/A building. I have the exact x,y coordinates for the buildings.
My main problem is that I don't really know how to set up the touch event on the UILabel and go to a new view controller, with parameters.
I guess I need to make in my mapviewcontroller new initiation methods to be able to get parameters and display them.
So to summarize: how can I navigate to an other view controller with touch event and how can I initiate that view controller with parameters. It's important that I can reach my map without any buildings parameters as well from my menu.
Any help appreciated.
Alternatively you can also add a hidden button over your label. This button will handle the touch event.
Use Tap gesture for it
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
//Create a Tap Gesture and set it on your Label.
UILabel *yourLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setTextAlignment:NSTextAlignmentCenter];
[yourLabel setText:#"Address"];
[self.view addSubview:yourLabel];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(TapGestureRecognizerLabel:)];
[tap setNumberOfTouchesRequired:1];
[yourLabel addGestureRecognizer:tap];
-(void)TapGestureRecognizerLabel:(UIGestureRecognizer *)gestureRecognizer
{
[self.navigationController pushViewController:targetControllerObj animated:YES];
}

Adding and removing dynamically generated buttons in xcode

This is a strange one.
I'm quite new to Xcode. I've been trying to make a simple app that adds new buttons when you single click on the button view and removes them when you double click.
Adding buttons is OK, but removing them is unreliable. I think it has something to do with the way I've written the code because it only seems to remove the most recently added button on double click and not the actual button I've clicked on.
My abridged .m Code is below:
- (void)viewDidLoad
{
- (void)handleSingleTap:(UITapGestureRecognizer *)tapper {
//adds the buttons and gives them a unique tag
ButtonCount = ButtonCount+1;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 150);
btn.userInteractionEnabled = YES;
btn.tag=PuckCount;
//attaches double tap recognizer to button
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleDoubleTap:)];
[doubleTapGestureRecognizer setNumberOfTapsRequired:2];
[btn addGestureRecognizer:doubleTapGestureRecognizer];
//Add Tap Recognizer to pucks to create new buttons
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSingleTap:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
[btn addGestureRecognizer:singleTapGestureRecognizer];
UIImage *buttonImage = [UIImage imageNamed:#"puck2.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
}
//handles what happens on a double tap - THIS IS WHERE I THINK THE PROBLEM IS.
- (void)handleDoubleTap:(UIGestureRecognizer *)doubletap{
PuckSelected = self.view.tag;
[[self.btn viewWithTag:PuckSelected] removeFromSuperview];
}
#end
If you are trying to remove the button that is being double tapped, then try this.
- (void) handleDoubleTap:(UIGestureRecognizer *) doubletap {
[doubletap.view removeFromSuperview];
}
The UIGestureRecognizer has the view attached to it. There is no need to retrieve it again. Plus your problem was that you were trying to retrive the view from self instead of doubletap
Your handleDoubleTap method doesn't make sense.
Do something like this ::
- (void)handleDoubleTap:(UIGestureRecognizer *)doubletap{
PuckSelected = doubletap.view.tag;
[[self.view viewWithTag:PuckSelected] removeFromSuperview];
}
And it will work fine
- (void)handleDoubleTap:(UIGestureRecognizer *)doubletap{
PuckSelected = self.view.tag;
[[self.btn viewWithTag:PuckSelected] removeFromSuperview];
}
According to the code above, the button that gets removed it the button that has the same tag as self.view. But you don't show how self.views tag gets set, so it's impossible for us to know why or how your code selects the button that will be removed.
Regardless, you're probably heading down the wrong road here. It looks like you might have buttons nested inside a button? Which is odd. Also, you're using gesture recognizers with UIButtons, which don't need it. They already respond to things like taps and double-taps via the target-action mechanism.
Generally speaking, controls (that is, objects that derive from UIControl) already handle touches, taps, and other simple interactions. You'd really only use gesture recognizers on UIViews that need to track swipes or pinches or the like.
There seem to be 2 problems with your code.
1. When you do a single tap you are adding a button at (0,0,150,150) so any new button that gets added will be on top of the previous button. Maybe I am seeing this because of your abridged code, but you may want to do something about that.
2. In the UITapgesturerecognizer it will give you the view(id) that was tapped. You should use that in your selector. Like:
doubletap.view.tag.

Resources