UIWebView touch event not getting fired - iphone

I am new to iPhone Developer,
I want to detect touch in my webview so i tried this but this method is not getting fired,
in .h file
#interface EpubReaderViewController : UIViewController
in .m file
...
- (void)sendEvent:(UIEvent *)event{
NSLog(#"Touch detected") ;
}
...
Even i tried this also,
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(#"Touches began");
}
Thanks In Advance !

User is going to zoom in and zoom out on the website, as well as touch to click hpyerlinks etc. That'w shy touch methods won't respond for UIWebView.
If you want to enable touch, you might need to subclass UIWebview while it is forbidded in developer docs, check following threads for more discussion-
Handling touches inside UIWebview
https://stackoverflow.com/questions/2122745/how-to-detect-touch-event-in-uiwebview
How to intercept touches events on a MKMapView or UIWebView objects?
Handling touches inside UIWebview
You can go through this tutorial as well -
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way

Related

iphone - the order of the touch (tap) event

I have a UIView and a UIWebView.
The UIWebView is a child view of the UIView.
The UIWebView contains a youtube video and is set as to let the video fit to the UIWebView.
I have a UITapGesture associated with the parent UIView for single tap, say, if a user single tap the whole view, it will invoke A.
So, when the UIWebView loads the youtube video, there is a button on top of the video waiting for users to click to play. But now if I click the button, the youtube is played, but also the A is invoked too. This is what I don't want.
How should I solve it?
I thought the touch/tap event should be in a order and if the button is clicked, it should absorb the event and not give the UIView any more.
I also tried to add another UIView under the UIWebView, and attach that gesture to the underlying view. However, it still doesn't work.
How can I do to let the button over the youtube video independent?
thanks
Try add this code in your view's .m file (if you are using UIView, subclass it):
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint p = [self convertPoint:point toView:webView];
if ([webView pointInside:p withEvent:event]) {
return [webView hitTest:p withEvent:event];
}
return [super hitTest:point withEvent:event];
}
Since I did not try this code, tell me if it doesn't work. There are still many ways to deal with the situation. :)

How to implement touch events in uiwebview?

I have tried various solutions provided on this site and others to implement touch events on uiwebview. But still I am not able to do this. Actually, i have created a article reader application. Here, I have added a uiwebview on the normal uiview. Now, I want to trace some user touch events on that particular webview.
When I do this on normal view, it works perfectly. But if I try it on webview. it stops working.
The solutions I tried before are
implementing touch methods like
touchbegan
touchended
touchmoved
touch cancelled
2 implementing uigesturerecognizer
3 implementing window touch events like send event
Now If anyone can help me or tell me where I am doing wrong or a new solution(other than this), then I will be thankful.
Put a transparent UIVIew on top of your UIWebView to capture touches. You can then act on them or optionally pass them down to the UIWebView using the touchesBegan deletage method.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.myUIWebView touchesBegan:touches withEvent:event];
}
Check this previous post for details: iOS - forward all touches through a view
I subclassed UIWebView and just "leeched" onto its gesture recognizers in subviews 2 levels deep (you could go recursively but thats enough for iOS6-7).
Then you can do whatever you want with the touch location and gesture recognizer's state.
for (UIView* view in self.subviews) {
for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
[recognizer addTarget:self action:#selector(touchEvent:)];
}
for (UIView* sview in view.subviews) {
for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
[recognizer addTarget:self action:#selector(touchEvent:)];
}
}
}

imagemap in objective c

i want to make something like this
http://www.htmlcodetutorial.com/images/images_famsupp_220.html
and i want to know if there is class to do it in objective c?
thx
if i understood you correctly, you want to have several sensitive points on an image view?
try adding
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(#"Touches: %#", touches); //examine this!
}
to your code.
it provides UITouch instances in the touches-set, each having an location point for your views.
checkout
UITouch locationInView:;
regs.
UIWebView supports image maps. If you want a pure Objective-C solution I would recommend using an UIImageView and capture touches where you would like to map the image. Once some one touches the view you can then call the appropriate action.

Passing touch events to iOS status bar

I have a UIWindow with windowLevel set to UIWindowLevelStatusBar+1. This window has a single semi-transparent view that blocks the status bar. I need to sometimes pass touch events from the view on to the status bar. Any ideas?
So, it seems to be doable with a custom subclass of UIWindow overriding hitTest:withEvent: that manually detects a touch in the subview, and always returns nil.
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([event type]==UIEventTypeTouches) {
UIView *v=[super hitTest:point withEvent:event];
if (customSubViewthatCoversStatusBarOnly==v)
//doLotsOfCoolStuff
}
return nil;
}
Status bar recognizes all touches, so there is no breakage with scroll-to-top, return to call, VoiceOver, etc.. And I still get to intercept taps on statusbar.
I hacked this up just now. I will probably upload an update to App Store later this week with a more mature version of this, will see how much complaining Apple will do.
EDIT - 7th April:
Was approved by Apple. Works flawlessly.
You might find this component over on github helpful.
Otherwise, Cocoa with Love blog post is really useful to read perhaps.
As far as I understand this, you should use - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event to implement that behavior. Basically, you either return self if you want to handle the touch event or [super hitTest:point withEvent:event] to let the status bar handle the touch event.
Check out the UIView Class Reference for more.
EDIT: As Jonathan mentioned, Apple might not approve this.

Detecting hits in UIScrollView while still letting it do it's thing

Is there a simple way to detect if someone touched a UIScrollView without having to disable user interactions?.
I know this has been answered a few times before, but every answer I find is somebody wanting to detect hits in an image. I don't have an image. What I do have is a scroll view with a number of text fields embedded in it. They used to be inside a UIControl, from which I could detect a touchDown and call resignFirstResponder on all my textfields (for when the keyboard is up). But when they are in a UIScrollView, I can't seem to find a simple way to do this.
I don't really want to have to write code to do the scrolling myself, which is what I assume I have to do if I disable user interactions and grab the touchesBegan message. But I may be wrong. I'm still a little new at this, but this is the first time I haven't been able to figure out the answer by reading the code doc and googling ...
Thanks for any help,
J
Override touchesBegan, do your processing, and call the super implementation so that the scroll view still gets the message.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//call resignFirstResponder
[super touchesBegan:touches withEvent:event];
}
Ok, now I feel a little silly answering this myself. I'm sure the other suggested answer would have worked also, but I found something else while browsing in other topics which totally fixed this for me.
I added this to my viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(backgroundTap:)];
[scrollView addGestureRecognizer: singleTap];
Where backgroundTap is
- (IBAction)backgroundTap:(id)sender
is the function I was originally calling from touchDown in UIControl to resign all keyboard firstResponders.
This works perfectly and adds very little code. And I DON'T have to disable user interactions! Yay!
Thanks all for your comments and help.
J
I fiddled with this for a while, and found that simple touchesBegan and touchesEnd type stuff didn't really work.
They don't work because because of the gestures. For example touchesEneded never fires when you swipe to scroll (at least not in the UIScrollView). So while you can detect when a touch event starts, you'll never know when it ends.
I wasn't real keen on including a gesture recognizer for simple stuff and discovered that you can find out about the state of the scrolling better from interacting with the UIScrollView directly.
And it's not that painful. Just add the protocol, then add the functions you need for detection.
In the view (class) that contains the UIScrolView, add the protocol, then added any the functions from here to your view (class).
Example:
// --------------------------------
// In the "h" file:
// --------------------------------
#interface myViewClass : UIViewController <UIScrollViewDelegate> // <-- Adding the protocol here
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView;
// --------------------------------
// In the "m" file:
// --------------------------------
#implementation BlockerViewController
UIScrollView *scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(#"end decel");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(#"end dragging");
}
// All of the available functions are here:
// https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html