Detecting taps inside a UIWebView - iphone

I have a UIWebView with a navigation bar and toolbar that I want to auto hide. I'm doing that already, but I want to show them when the user taps on the UIWebView.
My problem is that the UIWebView captures all the touches events, and I cannot intercept the one I need.
Is there any workaround for this?

Taken from a blog entry (doesn't load, but thankfully it's Google Cache'd) by Mithin Kumar.
(Mithin, I hope you don't mind me reposting it here; if you do, please let me know in the comments and I will remove it.)
Recently, I was working on a project which required detection of tap and events on the UIWebView. We wanted to find out the HTML element on which the user taps in the UIWebView and then depending on the element tapped some action was to be performed. After some Googling, I found out the most of the users lay a transparent UIView on top of the UIWebView, re-implement the touch methods of UIResponder class (Ex: -touchesBegan:withEvent:) and then pass the events to the UIWebView. This method is explained in detail here. There are multiple problems with the method.
Copy/Selection stops working on UIWebView
We need to create a sub-class of UIWebView while Apple says we should not sub-class it.
A lot other UIWebView features stop working.
We ultimately found out that the right way to implement this is by
sub-classing UIWindow and re-implementing the -sendEvent: method. Here
is how you can do it. First, create a UIWindow sub-class
#import <UIKit/UIKit.h>
#protocol TapDetectingWindowDelegate
- (void)userDidTapWebView:(id)tapPoint;
#end
#interface TapDetectingWindow : UIWindow {
UIView *viewToObserve;
id <TapDetectingWindowDelegate> controllerThatObserves;
}
#property (nonatomic, retain) UIView *viewToObserve;
#property (nonatomic, assign) id <TapDetectingWindowDelegate> controllerThatObserves;
#end
Note that we have variables which tell us the UIView on which to
detect the events and the controller that receives the event
information. Now, implement this class in the following way
#import "TapDetectingWindow.h"
#implementation TapDetectingWindow
#synthesize viewToObserve;
#synthesize controllerThatObserves;
- (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate {
if(self == [super init]) {
self.viewToObserve = view;
self.controllerThatObserves = delegate;
}
return self;
}
- (void)dealloc {
[viewToObserve release];
[super dealloc];
}
- (void)forwardTap:(id)touch {
[controllerThatObserves userDidTapWebView:touch];
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
if (viewToObserve == nil || controllerThatObserves == nil)
return;
NSSet *touches = [event allTouches];
if (touches.count != 1)
return;
UITouch *touch = touches.anyObject;
if (touch.phase != UITouchPhaseEnded)
return;
if ([touch.view isDescendantOfView:viewToObserve] == NO)
return;
CGPoint tapPoint = [touch locationInView:viewToObserve];
NSLog(#"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%f", tapPoint.x],
[NSString stringWithFormat:#"%f", tapPoint.y], nil];
if (touch.tapCount == 1) {
[self performSelector:#selector(forwardTap :) withObject:pointArray afterDelay:0.5];
}
else if (touch.tapCount > 1) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(forwardTap :) object:pointArray];
}
}
#end
Implement the sendEvent method in the above way, and then you can send
back the information you want back to the controller.
There are few things that one needs to keep in mind. Make sure in your
MainWindow.xib file, the window is of type TapDetectingWindow and not
UIWindow. Only then all the events will pass through the above
re-implemented sendEvent method. Also, make sure you call [super
sendEvent:event] first and then do whatever you want.
Now, you can create your UIWebView in the controller class in the
following way
#interface WebViewController : UIViewController<TapDetectingWindowDelegate> {
IBOutlet UIWebView *mHtmlViewer;
TapDetectingWindow *mWindow;
}
- (void)viewDidLoad {
[super viewDidLoad];
mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];
mWindow.viewToObserve = mHtmlViewer;
mWindow.controllerThatObserves = self;
}
Remember you’ll need to write the method userDidTapWebView in your
controller class. This is the method that is called in order to send
the event information to the controller class. In our case above we
are sending the point in the UIWebView at which the user tapped.

You can very simply use a UITapGestureRecognizer to detect tap gestures on a UIWebView. You must implement a UIGestureRecognizerDelegate method to allow the simultaneous recognition however.
- (void)viewDidLoad{
[super viewDidLoad];
UITapGestureRecognizer *targetGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
targetGesture.numberOfTapsRequired = 2;
targetGesture.delegate = self;
[self.webView addGestureRecognizer:targetGesture];
}
// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
//
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
NSLog(#"%#", otherGestureRecognizer);
//if you would like to manipulate the otherGestureRecognizer here is an example of how to cancel and disable it
if([otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer*)otherGestureRecognizer;
if(tapRecognizer.numberOfTapsRequired == 2 && tapRecognizer.numberOfTouchesRequired == 1){
//this disalbes and cancels all other singleTouchDoubleTap recognizers
// default is YES. disabled gesture recognizers will not receive touches. when changed to NO the gesture recognizer will be cancelled if it's currently recognizing a gesture
otherGestureRecognizer.enabled = NO;
}
}
return YES;
}
-(void)handleTap:(id)sender{
}

I had several issues trying to use brian.clear's answer (copied from a extinct post by Mithin Kumar) in my iOS 5-7, universal, storyboard-based, totally ARC project so I had to make several changes. I also improved some things and made it easier to understand (at least for me). If you are having trouble trying to use that answer from 2009 maybe you should try my update. Detailed instructions:
1. Create a new TapDetectingWindow class
TapDetectingWindow.h
// Created by Cristian Perez <cpr#cpr.name>
// Based on https://stackoverflow.com/a/1859883/423171
#import <UIKit/UIKit.h>
#protocol TapDetectingWindowDelegate
- (void)userDidTapView:(CGPoint)tapPoint;
#end
#interface TapDetectingWindow : UIWindow
#property (nonatomic) UIView *tapDetectingView;
#property (nonatomic) id <TapDetectingWindowDelegate> tapDetectedDelegate;
#end
TapDetectingWindow.m
// Created by Cristian Perez <cpr#cpr.name>
// Based on https://stackoverflow.com/a/1859883/423171
#import "TapDetectingWindow.h"
#implementation TapDetectingWindow
#synthesize tapDetectingView;
#synthesize tapDetectedDelegate;
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
if (tapDetectingView == nil || tapDetectedDelegate == nil)
{
return;
}
NSSet *touches = [event allTouches];
if (touches.count != 1)
{
return;
}
UITouch *touch = touches.anyObject;
if (touch.phase != UITouchPhaseEnded)
{
return;
}
if (touch.view != nil && ![touch.view isDescendantOfView:tapDetectingView])
{
return;
}
CGPoint tapPoint = [touch locationInView:tapDetectingView];
NSString *tapPointStr = NSStringFromCGPoint(tapPoint);
if (touch.tapCount == 1)
{
[self performSelector:#selector(notifyTap:) withObject:tapPointStr afterDelay:0.4];
// Make the afterDelay value bigger in order to have more chances of detecting a double tap and thus being able to cancel the single tap event, or make it smaller if you don't care about double taps and want to get the tap event as soon as possible
}
else if (touch.tapCount > 1)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(notifyTap:) object:tapPointStr];
}
}
- (void)notifyTap:(NSString *)tapPointStr
{
CGPoint tapPoint = CGPointFromString(tapPointStr);
[tapDetectedDelegate userDidTapView:tapPoint];
}
#end
2. Check you have a window declared in your app delegate
You should have something like this in YourAppDelegate.h. Don't change the name of the property!
#interface YourAppDelegate : UIResponder <UIApplicationDelegate>
{
// ...
}
// ...
// The app delegate must implement the window property if it wants to use a main storyboard file
#property (nonatomic) UIWindow *window;
#end
3. Override the window property of your app delegate
Just like this, which should be in YourAppDelegate.m. Again, don't change the name of the method!
// Replace the default UIWindow property with a TapDetectingWindow
- (TapDetectingWindow *)window
{
static TapDetectingWindow *tapDetectingWindow = nil;
if (!tapDetectingWindow)
{
tapDetectingWindow = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
return tapDetectingWindow;
}
4. Set the delegate protocol in your view controller
Be sure to adopt the tap handling protocol in your MainViewController.h
#import "TapDetectingWindow.h"
#interface MainViewController : UIViewController <TapDetectingWindowDelegate, ...>
{
// ...
}
// ...
#end
5. Set up the tap detecting window and tap event handler
Specify your webview (actually any UIView should work) and tap event handler in your view controller's (void)viewDidLoad method
- (void)viewDidLoad
{
// ...
// Allow tap detection in webview
TapDetectingWindow *tapDetectingWindow = (TapDetectingWindow*)[(YouTubeRPAppDelegate*)[[UIApplication sharedApplication] delegate] window];
tapDetectingWindow.tapDetectingView = self.webView; // Your UIWebView
tapDetectingWindow.tapDetectedDelegate = self;
}
6. Handle the tap event as you wish
Just implement the userDidTapView method in your view controller
- (void)userDidTapView:(CGPoint)tapPoint
{
NSLog(#"Tap detected in webview at %#", NSStringFromCGPoint(tapPoint));
}

Most of the approaches deal with a complicated pair of UIView and UIWebView subclasses and overrode -touchesBegan:withEvent: etc. methods.
This JavaScript-based approach intercepts touches on the web DOM itself, and it seems like a clever way to sidestep the more complex process. I haven't tried it myself, but I'm curious to know the results, if you give it a shot.

I'm not sure of the exact implementation details but you need to subclass UIWindow and override sendEvent:. Then you can capture tap events and handle them accordingly, before they get down to the web view. Hope this points you in the right direction!

Create a UIView sub class containing the whole UIWebView. Then the event hitTest will be fired on touching the webview. Note: Don't put anything to the container except the webView.
#interface myWebViewContainer : UIView
... ...
#end
Then override the hitTest event:
-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
NSLog(#"Hit test %#", [event description]);
UIView * returnView = [super hitTest:point withEvent:event];
[delegate userDidTapWebView];// notify the delegate that UIWebView is being touched by some naughty user :P
return returnView;
}

A much simpler approach since iOS 3.2 is simply to use a UITapGestureRecognizer.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer* tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(userDidTapWebView)] autorelease];
[self.pdfWebView addGestureRecognizer:tapGestureRecognizer];
}
- (void)userDidTapWebView {
NSLog(#"TAP");
}

Related

How to handle events (Touch Up Inside/Outside) for UItableViewCell with a text field?

I have a custom UITableViewCell which has a text field inside it. I have created it using IB and have a custom class with it.
Now, my issue is that I want to setup the text field so that during text entry if the user clicks outside the text field (without hitting the return/done key on the keypad), the field resigns first responder. I understand, that do that I need to handle the Touch Up Inside Event. However my tableview class never receives this event even though I have done the connections. I am assuming that its because its not subclass of UIcontrol, which I cant make it as it needs to be UITableViewCel.
So whats the solution?? How do I receive these events??
Header File:
#import <UIKit/UIKit.h>
#interface MMSingleTextFieldCell : UITableViewCell <UITextFieldDelegate>
// Properties
#property (weak, nonatomic) IBOutlet UITextField *singleTextField;
// Methods
- (IBAction)eventTouchUpOutside:(id)sender;
- (IBAction)eventTouchUpInside:(id)sender;
#end
Class File:
#import "MMSingleTextFieldCell.h"
#implementation MMSingleTextFieldCell
#synthesize singleTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)eventTouchUpOutside:(id)sender {
[singleTextField resignFirstResponder];
}
- (IBAction)eventTouchUpInside:(id)sender {
[singleTextField resignFirstResponder];
}
I have just recently open sourced a project on Github that should make this all relatively easy to do. It includes a class that can be easily inserted into a cell and a sample project demonstrating its capabilities.
If you look in RootTableViewController's viewDidLoadMethod you will see that I am adding a gesture recognizer:
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
_tapGestureRecognizer.delegate = self;
_tapGestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:_tapGestureRecognizer];
Add the dismiss keyboard method:
- (void)dismissKeyboard {
[_textField resignFirstResponder];
}
Add a gesture recognizer callback (in RootTableViewController):
//see: http://stackoverflow.com/questions/7195661/why-is-uigesturerecognizer-being-called-on-my-textfield-clear-button
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([touch.view isKindOfClass:[UITextField class]] ||
[touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
return YES;
}
Of course, this means you have to make RootTableViewController adhere to the UIGestureRecognizerDelegate protocol (in the header file):
#interface RootTableViewController : UITableViewController<SATextFieldDelegate, UIGestureRecognizerDelegate>
If you want the user to scroll the table view and dismiss the keyboard implement the following table view delegate callback:
- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {
if (_textField.isFirstResponder) {
[self dismissKeyboard];
}
}
I believe this is the function you want.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (_textField.isFirstResponder) {
[self dismissKeyboard];
}
}
Try this:
1) Implement the UIGestureRecognizerDelegate protocol
2) In viewDidLoad for example, create the following
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hideKeyboard:)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
3) Now, implement the method from protocol from 1
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// Use this for allow some control to receive his natural tap event. You can use tap.cancelsTouchesInView = NO; in the 2 step instead of this, try both of then and choose on your own.
if (touch.view == <some control>) {
//NSLog(#"NO");
return NO;
}
//NSLog(#"YES");
return YES;
}
4) finally, implement the callback for tap
-(void) hideKeyboard:(id)sender{
if (<your edit text>.isEditing) {
[<your edit text> resignFirstResponder];
}
}
I hope this will help, or at least point you to the right direction

How to change the frame of UIKeyBoard programmatically in iOS

Well, i have gone through some decent goggling before posting this question but was unsuccessful in finding the right answers.
I cant really explain my entire app scenario here as it is a bit complex to explain. So, let me make this question very very simple. How can i change the frame of the UIKeyBoard.i.e. I want the UIKeyBoard to rotate or translate 90 degrees upwards to support my view position.
Is there a way out for me?
You can't change the default keyboard. You can, however, create a custom UIView to be used as keyboard replacement by setting it as inputView on, for example, a UITextField.
While creating a custom keyboard takes a bit of time, it works well with older iOS versions (inputView on the UITextField is available in iOS 3.2 and later) and supports physical keyboards (the keyboard is hidden automatically if one is connected).
Here's some sample code to create a vertical keyboard:
Interface:
#import <UIKit/UIKit.h>
#interface CustomKeyboardView : UIView
#property (nonatomic, strong) UIView *innerInputView;
#property (nonatomic, strong) UIView *underlayingView;
- (id)initForUnderlayingView:(UIView*)underlayingView;
#end
Implementation:
#import "CustomKeyboardView.h"
#implementation CustomKeyboardView
#synthesize innerInputView=_innerInputView;
#synthesize underlayingView=_underlayingView;
- (id)initForUnderlayingView:(UIView*)underlayingView
{
// Init a CustomKeyboardView with the size of the underlying view
// You might want to set an autoresizingMask on the innerInputView.
self = [super initWithFrame:underlayingView.bounds];
if (self)
{
self.underlayingView = underlayingView;
// Create the UIView that will contain the actual keyboard
self.innerInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, underlayingView.bounds.size.height)];
// You would need to add your custom buttons to this view; for this example, it's just red
self.innerInputView.backgroundColor = [UIColor redColor];
[self addSubview:self.innerInputView];
}
return self;
}
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// A hitTest is executed whenever the user touches this UIView or any of its subviews.
id hitTest = [super hitTest:point withEvent:event];
// Since we want to ignore any clicks on the "transparent" part (this view), we execute another hitTest on the underlying view.
if (hitTest == self)
{
return [self.underlayingView hitTest:point withEvent:nil];
}
return hitTest;
}
#end
Using the custom keyboard in some UIViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
CustomKeyboardView *customKeyboard = [[CustomKeyboardView alloc] initForUnderlayingView:self.view];
textField.inputView = customKeyboard;
}

call method from subclass of UIImageview

I have two .m files. The first is the main code, The second is a subclass of UIImageView so that i can detect touches.
In the main .m file I have added a progress bar and a customimageview both subviews of a scrollview.
What I need is that when a user touches the customimageview that the progress bar moves up and a double tap decreases the [Note: the customimageview has to have its touches recognised in the second .m because of them being in a subview of a scrollview and other controls are having to be handled]
In the main .m file I have a two methods:
- (void)pumpsingletap {
progbm.progress +=0.1;
}
- (void)pumpdoubletap {
progbm.progress -=0.1;
}
then in the subclassed uiimageview i have:
//inside touches method
if ([touch view].tag == 555) {
NSLog(#"pump touched");
switch ([allTouches count]) {
case 1: {
switch ([touch tapCount]) {
//---single tap---
case 1: {
NSLog(#"single pump touch");
[self performSelector:#selector(pumpsingletap) withObject:nil afterDelay:.4];
} break;
//---double tap---
case 2: {
NSLog(#"double pump touch");
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(pumpsingletap) object:nil];
[self performSelector:#selector(pumpdoubletap) withObject:nil afterDelay:.4];
} break;
}
}
}
}
So the NSlog's appear so the touch recognition isn't an issue. But the performSelector falls over. As the customimageview pumpsingletap doesnt work.
So how do i call the method in the subclass.
//update//
so I have added in the following code, in my subclass
mainMethod* callingMethod = [[mainMethod alloc] init];
[callingMethod performSelector:#selector(pumpsingletap) withObject:nil afterDelay:.4];
then in my main method for pumpsingletap i changed it to:
- (void)pumpsingletap {
NSLog(#"single pump method called");
progbm.progress +=0.1;
}
The NSLog for single pump method called appeared but the progress bar progbm - didn't move. so i have solved my calling issue - just need to now work out why the progress bar isnt moving!!
If I'm following the code correctly, you are performing the selector on self, but self is your derived UIImageView class (so you probably crash at that point?). You need to perform selector on the class in your main file. Pass a reference to that to your derived class. Alternately, you could create a delegate, implement it in your main class, pass your main class to the UIImageView and then call through the delegate (there are even other ways to do it (key-value observation), but one of these should work).
I'm not sure if this is the problem, but you don't need brackets around case statements. Also I don't get why you would make a switch within a switch. Just make an if-elseif-else statement, it'll probably be easier to understand.
Other than this, from what I understand, you have a view controller with both a progress bar and a customimageview as properties, and you have methods that should be called in response to certain actions, (tapping or double tapping the customimageview) but they're in the view controller. The usual way to solve this is by using the target action mechanism. UIControls implement the target action mechanism by encapsulating target-action pairs and storing them in a dictionary, keyed by the event type (UIControlEvent). Here's a slightly simpler version.
In the .h file for your subclass of UIImageView, before the #interface write this:
typedef enum {
ControlEventTap = 0,
ControlEventDoubleTap
} ControlEvent;
Then in the .m file add this before the #implementation:
#interface TargetActionPair : NSObject {
id target;
SEL action;
}
#property (nonatomic, assign) id target;
#property (nonatomic, assign) SEL action;
#end
#implementation TargetActionPair
#synthesize target, action;
#end
Then, add an NSMutableArray instance variable, (but not a property) and a - (void)setTarget:(id)t action:(SEL)a forEvent:(ControlEvent)e method to your customimageview implementation.
The method should look like this:
- (void)setTarget:(id)t action:(SEL)a forEvent:(ControlEvent)e {
TargetActionPair *tar_act = [[TargetActionPair alloc] init];
tar_act.target = t;
tar_act.action = a;
// actionsArray is the mutable array instance variable and must be allocated and set in the init method for customimageview.
[actionsArray replaceObjectAtIndex:(NSUInteger)e withObject:tar_act];
[tar_act release];
}
Then you can replace your touch handling code with:
if ([touch view].tag == 555) {
NSUInteger tapcount = [touch tapCount];
if (([alltouches count] == 1) && (tapcount <= [actionsArray count])) {
TargetActionPair *tar_act = [actionsArray objectAtIndex:tapcount-1];
[tar_act.target performSelector:tar_act.action withObject:nil afterDelay:.4];
if (tapcount == 2) {
TargetActionPair *tar_act2 = [actionsArray objectAtIndex:tapcount-2];
[NSObject cancelPreviousPerformRequestsWithTarget:tar_act2.target selector:tar_act2.action object:nil];
}
}
}
With this code you simply set the target and action for each control event in the viewDidLoad method of the view controller that contains the customimageview. So the calls would look like this:
[self.customimageview setTarget:self action:#selector(pumpsingletap) forEvent:ControlEventTap];
[self.customimageview setTarget:self action:#selector(pumpdoubletap) forEvent:ControlEventDoubleTap];
DON'T FORGET to release the actionsArray in your dealloc method and to be very careful about releasing the view controller since the customimageview doesn't retain it.
I hope this helps, best of luck on your app.
In the end I solved the issue by using NSNotificationCenter

Capture gesture events on MKOverlayView with an UIGestureRecognizer

I try to capture events on a subclassed MKOverlayView with a UIGestureRecognizer.
However the selector never gets fired. Any ideas?
interface:
#import <MapKit/MapKit.h>
#interface XYOverlayView : MKCircleView {}
-(void) viewTapped:(UIGestureRecognizer *)gestureRecognizer;
#end
imp:
#implementation XYOverlayView
- (id)initWithOverlay:(id <MKOverlay>)overlay
{
if(self = [super initWithOverlay:overlay])
{
self.userInteractionEnabled = TRUE;
self.multipleTouchEnabled = TRUE;
UITapGestureRecognizer *tapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)];
[self addGestureRecognizer:tapRecogniser];
[tapRecogniser release];
}
return self;
}
-(void) viewTapped:(UIGestureRecognizer *)gestureRecognizer
{
NSLog(#"XYOverlayView tapped");
}
#end
you forgot to implement the UIGestureRecognizerDelegate in your interface and you have to import the UIKit
#import <UIKit/UIKit.h>
#interface XYOverlayView : MKCircleView <UIGestureRecognizerDelegate>
Make sure to specify the numberOfTapsRequired and numberOfTouchesRequired otherwise the gesture recognizer doesn't know what to accept as a 'tap'.
tapRecogniser.numberOfTapsRequired = 1;
tapRecogniser.numberOfTouchesRequired = 1;
Did not get this to work. Switched to an custom MKAnnotationView to capture the gestures. This also has the advantage the tapable region in the window stays the same size.
I think my answer here might be a solution: MKOverlayView and touches

Double Tap inside UIScrollView to Another View

I want to preface this question with the fact I am new to the iphone application development and many times believe I may be over my head. I am trying to find an action that will allow me to double tap anywhere within my full screen UIScrollView to return to my main menu (MainMenuViewController).
I currently have the following code running for my ScrollViewController...
ScrollViewController.h
#import <UIKit/UIKit.h>
#interface ScrollViewController : UIViewController <UIScrollViewDelegate>
{
}
#end
ScrollViewController.m
#import "ScrollViewController.h"
UIScrollView *myScrollView;
UIPageControl *myPageControl;
#implementation ScrollViewController
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[myScrollView subviews] count];
CGRect bounds = myScrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[myScrollView addSubview:page];
}
...etc
Any advice or sample code to implement a double tap within the ScrollView Controller to allow me to return to my MainMenuViewController would be greatly appreciated. Also please include any Interface Builder changes to the view (if necessary). I have been pouring over the forums for days and have not been able to successfully implement this action. Thanks...R.J.
First of all, create a subclass of a UIScrollView:
#import <UIKit/UIKit.h>
#interface MyScrollView : UIScrollView {
}
#end
Implement it:
#import "MyScrollView.h"
#implementation MyScrollView
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
- (void)dealloc {
[super dealloc];
}
#end
Then, use this subclass instead of a normal UIScrollView in your main View Controller. This will call the touchesEnded:withEvent: method (alternatively, you can call any method you want). Then, track for double taps (if you need info on how to do that, let me know).