I have a view controller with 3 UIWebViews, all with the same frame overlaying each other. I use three buttons to switch between them (similar to a segment control). I want to attach a tap gesture recognizer to the webview so that tapping each webview leads to a new controller. However, only the UITapGestureRecognizer for the first webview fires. Can anyone tell me how to get the other two gesture recognizers to fire? This is my code:
- (IBAction)changeWebView:(id)sender
{
UIButton *button = (UIButton *)sender;
switch (button.tag) {
case 0:
{
[self.view bringSubviewToFront:WebView1];
for (UITapGestureRecognizer *recognizer in WebView2.gestureRecognizers) {
[WebView2 removeGestureRecognizer:recognizer];
}
for (UITapGestureRecognizer *recognizer in WebView3.gestureRecognizers) {
[WebView3 removeGestureRecognizer:recognizer];
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showFullScreen1)];
tapGestureRecognizer.delegate = self;
[WebView1 addGestureRecognizer:tapGestureRecognizer];
}
break;
case 1:
{
[self.view bringSubviewToFront:WebView2];
for (UITapGestureRecognizer *recognizer in WebView1.gestureRecognizers) {
[WebView1 removeGestureRecognizer:recognizer];
}
for (UITapGestureRecognizer *recognizer in WebView3.gestureRecognizers) {
[WebView3 removeGestureRecognizer:recognizer];
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showFullScreen2)];
tapGestureRecognizer.delegate = self;
[WebView2 addGestureRecognizer:tapGestureRecognizer];
}
break;
case 2:
{
[self.view bringSubviewToFront:WebView3];
for (UITapGestureRecognizer *recognizer in WebView1.gestureRecognizers) {
[WebView1 removeGestureRecognizer:recognizer];
}
for (UITapGestureRecognizer *recognizer in WebView2.gestureRecognizers) {
[WebView2 removeGestureRecognizer:recognizer];
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showFullScreen3)];
tapGestureRecognizer.delegate = self;
[WebView3 addGestureRecognizer:tapGestureRecognizer];
}
break;
default:
break;
}
}
This sounds like a design issue. Why don't you only have one one UIView and render the correct content depending on what button was pressed. You can then have only one gesture recognizer that will load the proper controller (or perform the correct segue) also depending on the button.
Add UITapGestureRecognizer to a a view and add all 3 UIWebview as a subview on them.Than whenever you detect UITapGestureRecognizer to be tapped get the tag from segmentedController or button whatever you are using and find which one was tapped.
Don't add UITapGestureRecognizer and remove it again and again.
Related
I added tapGesture for self.view and UILabel (subview of mainView), each performs different selectors.
But the only main view tapGesture is being called and label tapgesture is not being called. How it is handled?
Here is the code:
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[lbl addGestureRecognizer:tapLblClick];
For two selectors only one method is called tapGesForSelf.
Here lbl is the subview of self.view.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
[_lbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[_lbl addGestureRecognizer:tapLblClick];
[tapGesForSelf requireGestureRecognizerToFail:tapLblClick];
}
- (void)tapGesForSelf:(UITapGestureRecognizer *)gesture
{
NSLog(#"self");
}
- (void)tapGesForLbl:(UITapGestureRecognizer *)gesture
{
NSLog(#"label");
}
I post answer for your question now once I tried and it worked well.
First see the label in design.I set label text as "Tap Me"
Now I set the code for view and label
- (void)viewDidLoad
{
[super viewDidLoad];
//TapGesture for Label
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapLabel:)];
tapLabel.delegate = self;
tapLabel.numberOfTapsRequired = 1;
lblTapMe.userInteractionEnabled = YES;
[lblTapMe addGestureRecognizer:tapLabel];
//TapGesture for View
UITapGestureRecognizer *tapMainView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapMainView:)];
tapMainView.delegate = self;
tapMainView.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapMainView];
}
//Action method for Label
-(void)actionTapLabel:(UITapGestureRecognizer *)gestureOnLabel{
UILabel *label = (UILabel *)gestureOnLabel.view;
NSLog(#"Lable text is - %#",label.text);
}
//Action method for View
-(void)actionTapMainView:(UITapGestureRecognizer *)gestureOnMainView{
NSLog(#"The Main view is tapped");
}
Output Screenshot
Please setUserInteractionEnabled:YES for interact the TapGesture
[lbl setUserInteractionEnabled:YES];
I have three UIImageview. I set the action for UIImageview by using tapGesture. I want each UIImageview to have a different action. How can I check that which UIImageview is presently clicked?
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTap setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTap];
[_imageTwo addGestureRecognizer:singleTap];
[_imageThree addGestureRecognizer:singleTap];
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
}
You can detect using below code:
-(void)singleTapping:(UITapGestureRecognizer *) recognizer{
if (recognizer.view == _imageOne){
} else if (recognizer.view == _imageTwo) {
}else
{
}
}
A UIGestureRecognizer is to be used with a single view. There is this explicit information in the Apple documentation:
Gesture Recognizers Are Attached to a View
Every gesture recognizer is associated with one view. By contrast, a
view can have multiple gesture recognizers, because a single view
might respond to many different gestures. For a gesture recognizer to
recognize touches that occur in a particular view, you must attach the
gesture recognizer to that view.
Firstly, you can't attach same UITapGestureRecognizer to more than one view.
So, your code will not work and you have to create three UITapGestureRecognizer instances and have to attach it to views like below:
UITapGestureRecognizer *singleTapImg1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg1 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg2 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg3 setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTapImg1];
[_imageTwo addGestureRecognizer:singleTapImg2];
[_imageThree addGestureRecognizer:singleTapImg3];
Now, you can use below code to get, which imageview clicked and perform action according to that:
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
if (recognizer.view == _imageOne){
//_imageOne tapped
}
else if (recognizer.view == _imageTwo) {
//_imageTwo tapped
}else{
//_imageThree tapped
}
}
I am working on app in which I want both short long gesture in same view, I added but issue is I'm facing is that short gesture end always call, Need your help how to do that in right way. Below is my code.
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self addGestureRecognizer:longGesture];
UILongPressGestureRecognizer *shortGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(shortGesture:)];
shortGesture.minimumPressDuration = 0.1f;
[shortGesture setDelegate:self];
[self addGestureRecognizer:shortGesture];
- (void)longGestureOnFormFields:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
- (void)shortGesture:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
You can manage that by using one UITapGestureRecognizer like,
self.view.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self.view addGestureRecognizer:longGesture];
UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(testm)];
[self.view addGestureRecognizer:recog];
and your methods smething like,
-(void)testm{
NSLog(#"tap");
}
-(void)longGestureOnFormFields : (UILongPressGestureRecognizer*)sender{
if (sender.state == UIGestureRecognizerStateEnded ) {
NSLog(#"long press gesture");
}
}
I have added gesture recognizer in viewDidload i.e in viewcontroller so i have add it to self.view if you have subclass UIVIew and adding this to that view then add it on self like [self addgesture...] as shown in question!
I am new in IOS Dev, was searching onclick event for UIView and i found this code that worked perfectly. But it doesnt call specific UIView as i am using multiple UIViews and want to add event on all of them separately while this code calls handleSingleTap onclick of either UIView. plz help..
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
NSLog(#"Clicked");
}
You can create UITapGestureRecognizer and add it to the views you want it to be clicked.
You can also set up tag property on each view to find out which view was clicked.
For example:
UITapGestureRecognizer *singleFingerTap1 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
view1.tag = 1;
[view1 addGestureRecognizer:singleFingerTap1];
UITapGestureRecognizer *singleFingerTap2 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
view2.tag = 2;
[view2 addGestureRecognizer:singleFingerTap2];
//..and so on
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
UIView *viewClicked = recognizer.view;
NSLog(#"Clicked: %d", viewClicked.tag);
}
The other solution is add one gesture recogniser to the main view (the view which hold the others view, something as you posted) and in handleSingleTap: method check if the point is inside the view:
BOOL isPointInsideView1 = [view1 pointInside:location withEvent:nil];
BOOL isPointInsideView2 = [view1 pointInside:location withEvent:nil];
But ic can failed when one view override another.
Need help using the AMSlideMenu from https://github.com/SocialObjects-Software/AMSlideMenu
In a project I'm creating I needed to disable the AMSlideMenu swiping so I could use the UISwipeGestureRecognize to perform another action.
The main swiping has been disabled [self disableSlidePanGestureForLeftMenu]; and the menu is accessible by pressing the menuBtn, but I would like to use UISwipeGestureRecognizer which AMSlideMenu seems to be disabling.
Any ideas on how I could regain the UISwipeGestureRecognizer for my project?
Thank you!
- (void)viewDidLoad
{
[super viewDidLoad];
[self disableSlidePanGestureForLeftMenu];
UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[menuBtn addTarget:self
action:#selector(menuButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:menuBtn];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipe];
}
- (void)menuButtonTapped:(id)sender
{
[self.mainSlideMenu openLeftMenu];
}
-(void) swipeRecognized:(UISwipeGestureRecognizer *)swipe {
int pageNum;
switch (swipe.direction)
{
case UISwipeGestureRecognizerDirectionLeft:{
pageNum++;
break;
}
case UISwipeGestureRecognizerDirectionRight:{
pageNum--;
break;
}
default:
break;
}
}
Change gesture in Table view
- (void)viewDidLoad
{
[super viewDidLoad];
[self disableSlidePanGestureForLeftMenu];
UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[menuBtn addTarget:self
action:#selector(menuButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:menuBtn];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.tableView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:rightSwipe];
}
- (void)menuButtonTapped:(id)sender
{
[self.mainSlideMenu openLeftMenu];
}
-(void) swipeRecognized:(UISwipeGestureRecognizer *)swipe {
int pageNum;
switch (swipe.direction)
{
case UISwipeGestureRecognizerDirectionLeft:{
pageNum++;
break;
}
case UISwipeGestureRecognizerDirectionRight:{
pageNum--;
break;
}
default:
break;
}
}
And now Just only Add an delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
You need to stop left/right pan gesture from left menu controller class.
And then put your swipe gesture for specific view controller.
Follow steps below to stop swipe gesture for left menu:
1) Go to AMSlideMenuMainViewController.m class, in that select - (void)setup method.
2) Comment following lines from this method will stop swipe gesture.
self.panGesture = [[UIPanGestureRecognizer alloc] init];
[self.panGesture addTarget:self action:#selector(handlePanGesture:)];
[self.currentActiveNVC.view addGestureRecognizer:self.panGesture];
3) After commenting these lines create a swipe gesture in your ViewController and handle your custom logic.