How can you detect the touch of the navigationBar title in iOS 7. The code below worked great prior to iOS 7 but no longer is the touch detected.
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
[[self.navigationController.navigationBar.subviews objectAtIndex:1] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:1] addGestureRecognizer:tapRecon];
thanks,
Greg
Can you print the description of self.navigationController.navigationBar.subviews objectAtIndex:1]? (Just NSLog it).
The same code works good for me:
UIView *viewWithTitleLabel = self.navigationController.navigationBar.subviews[1];
viewWithTitleLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureOccured:)];
[viewWithTitleLabel addGestureRecognizer:tapGesture];
When do you call your code? Seems to be that "subviews[1]" - is not your desired view.
TapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
NSInteger objIndex = 1;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.1f) objIndex = 2;
[[self.navigationController.navigationBar.subviews objectAtIndex:objIndex] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:objIndex] addGestureRecognizer:tapRecon];
In iOS 8 (and probably 7), the title object has the private class UINavigationItemView that you can check for and then apply your gesture recognition code more judiciously, rather than blindly using array indices.
for (UIView *view in self.navigationController.navigationBar.subviews)
{
if ([view isKindOfClass:NSClassFromString(#"UINavigationItemView")] &&
![view isKindOfClass:NSClassFromString(#"UINavigationItemButtonView")])
{
view.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectorGoesHere:)];
[view addGestureRecognizer:tapGesture];
break;
}
}
Whether or not this will pass app review is another question – one that I couldn't answer.
Related
I want to use tap gesture recognizer for detecting the user taps .i don't have much knowledge about this .Can anyone please help me for this .Tap Gesture .
I want to set the maximum count and i want to detect the user taps count if it is equal i want to perform some operation .Please help me to do this .
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
The above code is simple example of tea gesture recognizer .
Thanks in advance !!!!
Init Method :
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
Handle Method :
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
i=i+1;
if(i==10)
{
//Prforme task here
}
}
Declare i as global variable
Use this:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
// Your code goes here
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
self.imageforcapture.userInteractionEnabled = YES;
[self.imageforcapture addGestureRecognizer:tapGesture];
First, you need to allow imageView to receive touches by adding line above.
Define global variable i.
#property NSInteger i;
in viewDidLoad set the value of i to 0
self.i = 0;
After that you need to handle that global i variable that will track taps.
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
self.i++;
if (self.i == 10) {
//Perform your expected behaviour
}
}
I am trying to run a method by 2 times tapping on tv remote, consider tapping not clicking, but the touch surface does't recognize the taps. Instead, clicking two times runs the doubleTapping method.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
doubleTap.allowedTouchTypes =#[[NSNumber numberWithInteger:UITouchTypeIndirect]];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
- (void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"2 times");
}
}
I am missing something?
I was forgot to mention the UIPressType value , now due to position of remote's surface (Up / Down / Right / Left) you can now detect user's tap direction and add numberOfTapsRequired to the action :
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tapGestureRecognizer setAllowedPressTypes:#[#(UIPressTypeLeftArrow)]];
[tapGestureRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapGestureRecognizer];
I have read the UIPopoverController docs and more then 10 stackoverflow solutions which none work in iOS 8.0.2 ipad device.
THE GOAL:
I want to present a sharing view which when clicked outside dissmisses.
THE PROBLEM:
The code works fine below iOS 8.0.
THE CODE (tried many versions...):
NSArray *dataToShare = #[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.modalInPopover = YES;
self.act = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.act presentPopoverFromRect:CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, self.view.bounds.size.width/2, self.view.bounds.size.width/2) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
self.act.delegate = self;
I have added the :
UIPopoverControllerDelegate
WHAT I HAVE TRIED:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
As soon that the
UIPopoverController
is presented the view losses focus and doesn't register touches. Therefore, I can't handle touches outside the self.act rect space in order to force a dissmisal.
also...
NSArray *dataToShare = #[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.modalInPopover = YES;
UIPopoverPresentationController *presentationController = [activityViewController popoverPresentationController];
presentationController.sourceView = self.view; // if button or change to self.view.
[self.parentViewController presentViewController:activityViewController animated:YES completion:nil];
and I tried to present the UIActivityViewController and UIPopoverController from the parent which didn't work aswell.
Please help, I lost half day on something this stupid which works on iOS 7.1 and not on iOS 8+ which is ridiculous and I still have no clue why.
I will try to add the cancel button which has also disappeared in iOS8+.
UPDATE 1:
Adding a UIButton inside a UIBarButtonItem didn't help...
UIButton *control = (UIButton *) sender;
[control setFrame:CGRectMake(300, 300, 100, 100)];
[control setBackgroundColor:[UIColor redColor]];
UIBarButtonItem *barButtonItemView = [[UIBarButtonItem alloc] init];
[barButtonItemView setCustomView:control];
[self.popup presentPopoverFromBarButtonItem:barButtonItemView permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
UPDATE 2 - 19.12.2014. (iOS 7.1. - 8.1.2):
The accepted answer works on iOS 8.1. I had some errors with iOS 7.1. so I modified it to work for iOS 7.1. I have stated on which devices the code works in the comments below the answer.
// publish - sharing
NSArray *dataToShare = #[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSLog(#" SHARING - 1 - ");
[self presentViewController:activityViewController animated:YES completion:^{}];
}
else
{
// Change Rect to position Popover
NSLog(#" SHARING - 2 - ");
self.popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width * 3 / 5 ,
self.view.frame.size.width/2,
self.view.frame.size.width/10,
self.view.frame.size.width/10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
How about just add this.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.modalInPopover = YES;
activityViewController.popoverPresentationController.sourceView = self.view;
I have a class called PhotoBoothController that I use to manipulate an image using gestures. It works fine when I run it on iPhone. However when I run it on iPad, I display the PhotoBoothController inside a UIPopoverController. The image appears fine, however I can't manipulate it as my gestures don't appear to be recognized. I'm not sure how to make the UIPopoverController take control of the gestures.
I present the popovercontroller and configure the view thus:
- (void)presentPhotoBoothForPhoto:(UIImage *)photo button:(UIButton *)button {
//Create a photoBooth and set its contents
PhotoBoothController *photoBoothController = [[PhotoBoothController alloc] init];
photoBoothController.photoImage.image = [button backgroundImageForState:UIControlStateNormal];
//set up all the elements programmatically.
photoBoothController.view.backgroundColor = [UIColor whiteColor];
//Add frame (static)
UIImage *frame = [[UIImage imageNamed:#"HumptyLine1Frame.png"] adjustForResolution];
UIImageView *frameView = [[UIImageView alloc] initWithImage:frame];
frameView.frame = CGRectMake(50, 50, frame.size.width, frame.size.height);
[photoBoothController.view addSubview:frameView];
//Configure image
UIImageView *photoView = [[UIImageView alloc] initWithImage:photo];
photoView.frame = CGRectMake(50, 50, photo.size.width, photo.size.height);
photoBoothController.photoImage = photoView;
//Add canvas
UIView *canvas = [[UIView alloc] initWithFrame:frameView.frame];
photoBoothController.canvas = canvas;
[canvas addSubview:photoView];
[canvas becomeFirstResponder];
[photoBoothController.view addSubview:canvas];
[photoBoothController.view bringSubviewToFront:frameView];
//resize the popover view shown in the current view to the view's size
photoBoothController.contentSizeForViewInPopover = CGSizeMake(frameView.frame.size.width+100, frameView.frame.size.height+400);
self.photoBooth = [[UIPopoverController alloc] initWithContentViewController:photoBoothController];
[self.photoBooth presentPopoverFromRect:button.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
I thought [canvas becomeFirstResponder] might do it but it doesn't appear to make a difference.
Any advice much appreciate, thank you.
UPDATE: adding code as per comment
- (void)viewDidLoad {
[super viewDidLoad];
if (!_marque) {
_marque = [CAShapeLayer layer];
_marque.fillColor = [[UIColor clearColor] CGColor];
_marque.strokeColor = [[UIColor grayColor] CGColor];
_marque.lineWidth = 1.0f;
_marque.lineJoin = kCALineJoinRound;
_marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
_marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
_marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
}
[[self.view layer] addSublayer:_marque];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self.view addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[canvas addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:1];
[tapProfileImageRecognizer setDelegate:self];
[canvas addGestureRecognizer:tapProfileImageRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && ![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]];
}
I am working on a similar application, and i can easily manipulate images on the canvas.
1) Check if the userInteractionEnabled property of the canvas is set to YES.
2) You can try adding the gestures to the ImageView's rather than the canvas. (I am guessing u want to zoom , rotate n swipe the images).
some guesses:
is this because viewDidLoad method have been called before cavas property been set? In this case, you could try to add recognizers after present popover in another method such as viewDidAppear.
I also agree that the key may be that UIPopoverController allows interaction to other views outside of the popover. So i suggest to move [canvas becomeFirstResponder]; to the end of the viewDidLoad method in PhotoBoothController and try self.view becomeFirstResponder here because you actually add recognizers to self.view.
Im working on an app with zooming function.
In this app I have this button. I want it to respond to tapping in several ways:
Single tap: Zoom in slightly.
Double tap: Zoom in to the max.
Ive tried several options to achieve this but none are what I want.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[zoomin addTarget:self action:#selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];
Both work on single and double tap but when I press the button once to slightly zoom and seconds later I press it again it doesn't zoom in slightly, it zooms in to the max.
It is possible to fix this with a timer and location check so that when u tap and tap again u can be sure that the location is in a similar area and the taps happened within timer range.
But is this what I really need?
Is there a simpler solution?
the solution provided by omz is not good.
where as you can do this by simply adding these lines of code like posted here. Double-tap or two single-taps?
NOTE THE MAGICAL LINE : [tapRecg requireGestureRecognizerToFail:doubleTapRecg];
ABSTRACT:
UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTapped:)];
doubleTapRecg.delegate = self;
doubleTapRecg.numberOfTapsRequired = 2;
doubleTapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:doubleTapRecg];
UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapped:)];
tapRecg.delegate = self;
tapRecg.numberOfTapsRequired = 1;
tapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tapRecg];
[tapRecg requireGestureRecognizerToFail:doubleTapRecg];
[doubleTapRecg release];
[tapRecg release];
You can do it with two gesture recognizers and a timer:
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];
You'll have to use a slight delay in your tap: action before zooming in slightly because the first tap could be followed by a second tap:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
[self performSelector:#selector(singleTap) withObject:nil afterDelay:0.25];
}
- (void)singleTap
{
//slightly zoom in...
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
//Cancel the timer for the single tap action:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
//zoom in to the max zoom level...
}