How to stop handling Gestures - ios

In my app I have a tapGesture, panGesture, rotationGesture and pinchGesture.
The tapGesture is the starting point for all gestures, it shows me among other things which subview is selected.
After I entered a button to handle ImagePicker, the subview is still selected and therefore it is still handling gestures.
My question: Is there any statement to stop handling gestures?
EDIT
I don't need the gestureRecognizer, therefore I put them inactive:
panRecognizer.enabled = NO;
pinchRecognizer.enabled = NO;
rotationRecognizer.enabled = NO;
So if I need them I want to get them work when I am handling a tapRecognizer,
but here the recognizer do not move from inactive to active.
[panRecognizer isEnabled];
pinchRecognizer.enabled = YES;
rotationRecognizer.enabled = YES;
EDIT
My view is a ViewController and the subviews are on the imageView.
The recognizers are assigned to self.imageView.
In the first method I disenable the recognizers and in the second method I enable them
- (IBAction)photo: (id) sender {
panRecognizer.enabled = NO;
pinchRecognizer.enabled = NO;
rotationRecognizer.enabled = NO;
UIImagePickerController* picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = NO;
#try {
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
#catch (NSException * e) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Error" message:#"Camera is not available"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
[picker release];
}
- (IBAction)oneTap: (UIGestureRecognizer*)gestureRecognizer {
NSLog(#"oneTap");
float differenz = 2000;
[panRecognizer isEnabled];
pinchRecognizer.enabled = YES;
rotationRecognizer.enabled = YES;
for (UIView* const subview in array) {
subview.opaque = YES;
CGPoint const point = [gestureRecognizer locationInView:self.imageView];
float zwischenS = sqrt(powf(point.x - subview.frame.origin.x,2)) + sqrt(powf(point.y - subview.frame.origin.y,2));
if (differenz > zwischenS ) {
differenz = sqrt(powf(point.x - subview.frame.origin.x,2)) + sqrt(powf(point.y - subview.frame.origin.y,2));
newView.layer.borderColor = [[UIColor clearColor]CGColor];
newView = subview;
subview.layer.borderColor = [[UIColor whiteColor]CGColor];
subview.layer.borderWidth = 3.0f;
[imageView bringSubviewToFront: subview];
}
}
}
What is my mistake?
Thanks in advance

property [UIGestureRecognizer enabled]

//YOU CREATE GESTURES LIKE THIS
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture:)];
//YOU ADD THEM LIKE THIS
[self.view addGestureRecognizer:panGesture];
//AND YOU REMOVE THEM LIKE THIS
[self.view removeGestureRecognizer:panGesture];
I'm not sure how you are coding things, but I hope this gives you an idea

Related

Enlarge image when imageview inside a scrollview is tapped

I have an app that implements Horizontal gallery. I am able to display the image thumbnails. Now what I need to do is to enlarge the image when it is tapped. I am using EMNotificationPopup to display the enlarged image. Here's what I have so far:
This snippet is in my viewdidload:
UITapGestureRecognizer *singleTap;
UIImageView * imageview;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
[singleTap setNumberOfTouchesRequired:1];
[singleTap setNumberOfTapsRequired:1];
singleTap.delegate = self;
[imageview setUserInteractionEnabled:YES];
[imageview addGestureRecognizer:singleTap];
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
[self coreManagementWithPosition:EMNotificationPopupPositionCenter andType:EMNotificationPopupBig];
}
- (void) coreManagementWithPosition: (EMNotificationPopupPosition) position andType:(EMNotificationPopupType) notificationPopupType {
if (_notificationPopup.isVisible) {
[_notificationPopup dismissWithAnimation:YES];
_notificationPopup = NULL;
} else {
_notificationPopup = [[EMNotificationPopup alloc] initWithType:notificationPopupType enterDirection:EMNotificationPopupToDown exitDirection:EMNotificationPopupToLeft popupPosition:position];
_notificationPopup.delegate = self;
_notificationPopup.title = #"Sorry for this Alert message :)";
_notificationPopup.subtitle = #"Awesome message :)";
_notificationPopup.image = [UIImage imageNamed:#"alert_image"];
if (notificationPopupType == EMNotificationPopupBigButton)
_notificationPopup.actionTitle = #"OK";
[_notificationPopup show];
}
}
My issue here is handleImageTap is not being initialised/called. How can enable the user interaction for imageview.

Get identifier to which image tapped with dynamic images

I am trying to find which image the user tapped using a gesture recognizer.
The scrollview as well as the image views are all created dynamically.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
scroll.pagingEnabled = YES;
int iPolaroidDimen = 200;
NSInteger numberOfViews = 10;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *polaroid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"iconPol.png"]];
polaroid.frame = CGRectMake(xOrigin+(self.view.center.x/2)-(iPolaroidDimen/10), (self.view.center.y/2), iPolaroidDimen, iPolaroidDimen);
[scroll setShowsHorizontalScrollIndicator:NO];
polaroid.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[polaroid addGestureRecognizer:singleTap];
[polaroid setUserInteractionEnabled:YES];
[polaroid setAccessibilityIdentifier:[NSString stringWithFormat:#"%#%#",#"ImageView",[NSString stringWithFormat:#"%d",i]]];
[scroll addSubview:polaroid];
The gesture recognizer
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
}
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer {
UIView *tappedView = gestureRecognizer.view;
}
You can give a pointer to each of the 10 images so you can access them later. You only need to add tap gesture to the top view which has all the 10 images, and then add the following example code in your method (in which I give each image a different name such as cat, cow ,etc. :]):
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
CGPoint point = [gestureRecognizer locationInView:self.view];
CGRect catRect = CGRectMake(self.cat.frame.origin.x, self.cat.frame.origin.y, self.cat.frame.size.width, self.cat.frame.size.height);
CGRect cowRect = CGRectMake(self.cow.frame.origin.x, self.cow.frame.origin.y, self.cow.frame.size.width, self.cow.frame.size.height);
if (CGRectContainsPoint(catRect, point)) {
NSLog(#"I am a cat!");
} else if (CGRectContainsPoint(cowRect, point)) {
NSLog(#"I am a cow!");
}
}

How to disable UITapGestureRecognizer when sellect photo from library?

I'm using UITapGestureRecognizer
This is my code:
Home.m:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAnim:)];
[self.view addGestureRecognizer:tapGesture];
UIButton *buttontest = [[UIButton alloc] init];
buttontest.backgroundColor = [UIColor whiteColor];
buttontest.frame = CGRectMake(0, 80, 40, 40);
[buttontest addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttontest];
[self.view bringSubviewToFront:buttontest];
}
- (void)test: (UIButton*)aButton {
// TakePhoto *mvc = [[TakePhoto alloc]initWithNibName:#"TakePhoto" bundle:Nil];
// [self.navigationController pushViewController:mvc animated:YES];
//
// [self.view removeFromSuperview];
if (self.companyController) {
self.companyController = nil;
}
self.companyController = [[TakePhoto alloc] initWithNibName:#"TakePhoto" bundle:nil];
UIView *viewSuper = [[IQAppDelegate shareInstance] currentVisibleController].view;
UIViewController *profile = self.companyController;
profile.view.frame = viewSuper.frame;
[viewSuper addSubview:profile.view];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.size.height, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:0.35];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.origin.x, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void) tapAnim: (UITapGestureRecognizer*)gestureRecognizer {
// Show something
}
TakePhoto.m
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
I add a view:Takephoto front of Home(i'm not use "push"), like this:
--->Home
--->Take photo (like a popUp show): it has 2 buttons "Choose photo from library" and "Close"
When i use function "Choose photo from gallery", i cant choose a photo and UITapGestureRecognizer is always show.
How to disable UITapGestureRecognizer when choose photo from gallery?
P/S:sorry about my english.
Gesture recognizers have the enabled property. Set this to NO to disable the gesture recognizer. To make this easier you should keep a reference to the tap gesture recognizer using an instance variable.
you can set tapGesture.enabled=NO before you choose photos.
I think you need to implement this below delegate method :-
- (BOOL)gestureRecognizer:
(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
When you look on the documentation it will return YES (by default) to allow the gesture recognizer to examine the touch object, NO to prevent the gesture recognizer from seeing this touch object. For more details follow this https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldReceiveTouch:

How can I check with the recognizer, if I have a subview clicked?

When I click on a image I want to show another popover, but it doesn't work!
My handle Tap method looks like :
-(void)handleTapView:(UITapGestureRecognizer*)recognizer
{
CGPoint startPoint = [recognizer locationInView:recognizer.view];
NSLog(#"handle Tap VIEW!!!!!!!!");
if ([recognizer.view isKindOfClass:[UIImageView class]] ) {
NSLog(#"Tap Image!!!!!!!!");
}
else if ([self.popover isPopoverVisible]) {
[self.popover dismissPopoverAnimated:YES];
}
else {
ShapesListViewController *shapes = (ShapesListViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"ShapesListViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shapes];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:nav];
shapes.delegate = self;
self.popover = pop;
CGRect popoverRect;
popoverRect.origin = startPoint;
popoverRect.size.width = 1;
popoverRect.size.height =1;
[pop presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
In viewdidload :
ImageView *imgv = [[ImageView alloc] initWithImage:[UIImage imagNamed:#"delete.png"]];
imgv.center = CGPointMake(250,250);
[self.view addSubview:imgv];
ImageView *imgv2 = [[ImageView alloc] initWithImage:[UIImage imageNamed:#"gear.png"]];
imgv2.center = CGPointMake(400,400);
[self.view addSubview:imgv2];
//Tap Recognizer
self.singelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapView:)];
[self.singelTapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:self.singelTapGestureRecognizer];
You need to attach a separate gestureRecogniser to each view whose gestures you want to capture - in your case, both of your imageViews.
UITapGestureRecognizer *tapGR1, *tapGR2;
SEL selector = #selector(handleTapView:);
tapGR1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
tapGR2 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
[imgv1 addGestureRecognizer:tapGR1];
[imgv2 addGestureRecognizer:tapGR2];
Don't attach a tapGR to their superview.
Then you also need to set userInteractionEnabled on each of the imageViews otherwise they will ignore touches (UIImageView defaults to userInteractionEnabled = NO):
imgv1.userInteractionEnabled = YES;
imgv2.userInteractionEnabled = YES;
In you handleTapView you need to reorganise slightly. Change the else if in you conditional sequence to if otherwise the third clause will never get triggered.
The recognizer.view for each of the recongnizers will correctly identify the imageView that was tapped. That will be the rect that the popover should present from in the coordinates of the imageView's superview - it's frame property.
So:
[pop presentPopoverFromRect:recognizer.view.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];

Can't successfully removeGestureRecognizer iOS

I have a button in my iPad app, when its pressed it addGestureRecognizer's. When pressed again, its supposed to remove them.
My code is in an if block, and I know the right block of code is runned because NSLog print the right log message.
I'm adding the recognizers like this:
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:pinchRecognizer];
[self.view addGestureRecognizer:rotationRecognizer];
[self.view addGestureRecognizer:tapRecognizer];
And trying to remove them like this:
[self.view removeGestureRecognizer:panRecognizer];
[self.view removeGestureRecognizer:pinchRecognizer];
[self.view removeGestureRecognizer:rotationRecognizer];
[self.view removeGestureRecognizer:tapRecognizer];
This is my whole function:
http://www.pastelib.com/show/ZlICyb9Q
It does not work and I cant figure out why, do you guys have any suggestions?
Thanks in advance for all the help and suggestions :)
Your GestureReconizers are declared locally to the -showMenu: method.
So each time your button is pressed, you create new GestureReconizers, so you didn't try to remove previous reconizers, but you try to remove newly created ones.
Change the scope of your reconizers objects, and your problem will be fixed.
Cheers.
Edit with code sample :
-(IBAction) showMenu:(id) sender {
if([self.view.subviews containsObject:menuView]) {
NSLog(#"remove gestures!");
// Dismiss menu
[menuView removeFromSuperview];
// Remove gestures
[self.view removeGestureRecognizer:panRecognizer];
[self.view removeGestureRecognizer:pinchRecognizer];
[self.view removeGestureRecognizer:rotationRecognizer];
[self.view removeGestureRecognizer:tapRecognizer];
}else{
// Create menu
NSLog(#"add gestures!");
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
// Make gestures work simultaneously
panRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
// Set width and height if empty
self.menuController = [[menuController alloc] initWithNibName:#"menuController" bundle:nil];
if(screenWidth == 0) {
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(currentOrientation == 1 || currentOrientation == 2) {
// portrait
screenHeight = self.menuController.view.frame.size.height;
screenWidth = self.menuController.view.frame.size.width;
}else{
// Landscape
screenHeight = self.menuController.view.frame.size.width;
screenWidth = self.menuController.view.frame.size.height+20; //height+20px for the status bar
}
}
int menuHeight = 80;
self.menuController.view.frame = CGRectMake(0,screenHeight-(menuHeight-20), screenWidth, menuHeight);
self.menuView = self.menuController.view;
[self.menuController setDelegate:self];
[self addChildViewController:self.menuController];
[self.menuController didMoveToParentViewController:self];
[self.view addSubview:menuView];
// Add image gestures
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:pinchRecognizer];
[self.view addGestureRecognizer:rotationRecognizer];
[self.view addGestureRecognizer:tapRecognizer];
[self.view bringSubviewToFront:menuButton];
}
}
I did this in my project and it worked:
while (cellMainView.descripcionArticulo.gestureRecognizers.count) {
[cellMainView.descripcionArticulo removeGestureRecognizer:[cellMainView.descripcionArticulo.gestureRecognizers objectAtIndex:0]];
}

Resources