UIWebView with Touch Event - iphone

I need to be able to catch touch events on a UIWebView and still be able to use the webview's contents (html links, etc...)
I have subclassed UIWebView and implemented methods:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
I am now able to catch touch events, but the view has become ususable :(
I then tried to set the following on the webview object in my UIViewController, but that changed nothing.
[webview setDelegate:self];
[webview setUserInteractionEnabled:YES];
Can anyone help? Do you need further info?
Tia,
S.

I ended up not subclassing UIWebView and using a Gesture Recognizer on the object in the view controller. It works really well!
S.

In each of the functions you implemented did you also call super? For instance
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Your code here
[super touchesBegan:touches withEvent:event];
}
If don't call super UIWebView can't know the someone touched a link on it

Related

Handle events of objective c/c++ for the iphone devlopment using xcode 3.2

I am working as a developer for the iPhone applications, but i m stuck at certain point, can anyone tell me that how do i handle the events of C or C++ in Xcode 3.2 for the iPhone development?
If you want to handle events for a UIView or any class which is subclassed from UIResponder... Just overhide touch points and handle events there...
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded withEvent:event];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved withEvent:event];
}

how to detect touch on UIView with UIimage ontop of it?

i want to use a UIview and a UIImage ontop of it with a text box and button i want the user to drag it when the UIview or anything in the view is touched, what would be the best way to implement this method?
the UIView is on a single View Controller and using Interface Builder to make my UIviews and UIimages.
Have a look at the Apple Sample "MoveMe" http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html
The class MoveMeView in there is draggable. (There's also some superfluous animation going on, but the dragging is there).
You need to subclass UIView and implement
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event, and
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event.

A problem with override UINavigationController

now i'm working with cocos2d and i design to add navigationcontroller to my cocos2d application,
so i add navigationcontroller to my application when i click it not pass touch or event to cocos2d
now i'm try to override UINavigationController by
add new new class name is NavigationController and inherit from UINavigationController
in init i call [super init];
every things look be ok
but when i try to add
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Im overriding touch");
return YES;
}
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Im overriding touchMove");
return YES;
}
it not call
Why are you calling the methods ccTouchesBegan:withEvent: and ccTouchesMoved:withEvent: instead of the original names? You don't have to change the names of the methods when you subclass UINavigationController; instead, you should keep the same names and call super on them as well as appropriate. For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"I'm overriding touch");
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"I'm overriding move");
[super touchesMoved:touches withEvent:event];
}

How to enable user to click the View to hide the kb on iPhone?

I find that the View do not have a touch inside or similar event. So, I make a button on top on the view, and set it Alpha to 0, but after I set to Alpha 0, it cannot touch anymore. Any ideas on this?
You can use UIView subclass and handle touch event there. Methods to look at (defined in UIResponder):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
If you want to use "invisible" UIButton you should set its type to UIButtonTypeCustom - by default it will appear with no image and title and with transparent background. So technically it will be invisible to user but still be able to respond to all events.

Is there a way to pass touches through on the iPhone?

I have several UIButtons which I use to set the current action when tapping in the main area. I would also like to allow the user to drag from the button directly into the main area and take the same action; essentially, the touchesBegan and touchesMoved should be passed on to the main view when touching the UIButtons, but also should send the button press action.
Right now, I have the touch up inside changing the control. The drag exit calls the touch up inside section to set the control, then calls the touches began section to start the main area touching operation.
However, at this point, the touchesMoved and touchesEnded are obviously not being called, because the touches originated on the UIButton.
Is there a way to half-ignore the touches so they're passed to the main area, but also allow me to set the control first?
I know this question is two years old (and already answered), but nevertheless...
When I tried this myself, the touches were forwarded, but the buttons no longer behaved like buttons. I also passed the touches along to "super" and now all is well.
So, for beginners that might stumble upon this, this is what the code should look like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
In the documentation, look for Responder Objects and the Responder Chain
You can "share" touches between objects by forwarding the touch up the responder chain.
Your UIButton has a responder/controller that receives the UITouch events, my guess is that once it has preformed its interpretation of the message it returns - the touch has been handled and disposed of.
Apple suggests something like this (based on the type of touch of course):
[self.nextResponder touchesBegan:touches withEvent:event];
Rather than disposing of the touch event it is passed on.
Sub classed UIButton:
MyButton.h
#import <Foundation/Foundation.h>
#interface MyButton : UIButton {
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;
#end
MyButton.m
#import "MyButton.h"
#implementation MyButton
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
printf("MyButton touch Began\n");
[self.nextResponder touchesBegan:touches withEvent:event];
}
#end
No need to subclass either! Simply stick this on the top of your implementation before anything else:
#pragma mark PassTouch
#interface UIButton (PassTouch)
#end
#implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self.nextResponder touchesCancelled:touches withEvent:event];
}
#end