Tuesday 23 June 2009
Cocoa : Who to handle MouveXXX events on UIWebView
Par Vincent DEMAY, Tuesday 23 June 2009 :: IPhone
Default behavior of the UIWebView is : eat touchXXX events and not send them to the nextResponder.
One way to be able to catch this kind of event on a UIWebView is to add on top of it a UIView. But at this point, links, scroll, etc... are not capture to inner components of the under UIWebView. Here is a HACK to make this struff work :
//
// TouchUIWebView.h
//
// Created by Vincent Demay on 19/06/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import
@interface DummyTapView : UIView {
UIView * uiScroller_;
UIView * uiWebDocumentView_;
}
@property(nonatomic, retain) UIView *uiScroller;
@property(nonatomic, retain) UIView *uiWebDocumentView;
@end
@interface TouchUIWebView : UIWebView {
}
@end
//
// TouchUIWebView.m
//
// Created by Vincent Demay on 19/06/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TouchUIWebView.h"
const char* kUIScrollerName = "UIScroller";
const char* kUIWebDocumentView= "UIWebDocumentView";
@implementation DummyTapView
@synthesize uiScroller = uiScroller_;
@synthesize uiWebDocumentView = uiWebDocumentView_;
//
// retreive the scroller view in uiwebView
// used to compute scrolling in the loaded web page
//
- (UIView*) uiScroller {
if( self.superview !=nil && uiScroller_ == nil ) {
NSArray *views = [self.superview subviews];
for( id view in views ) {
const char* name = object_getClassName(view);
if( !strncmp( name, kUIScrollerName, strlen(name) ) ) {
uiScroller_ = view;
return uiScroller_;
}
}
}
return uiScroller_;
}
//
// retreive the document view in UiWebView
// used to compute touch on link etc...
//
- (UIView*) uiWebDocumentView {
if( self.superview != nil && uiWebDocumentView_ == nil ) {
NSArray *views = [self.uiScroller subviews];
for( id view in views ) {
const char* name = object_getClassName(view);
if( !strncmp( name, kUIWebDocumentView, strlen(name) ) ) {
uiWebDocumentView_ = view;
return uiWebDocumentView_;
}
}
}
return uiWebDocumentView_;
}
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor clearColor];
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.uiScroller touchesBegan:touches withEvent:event];
[self.uiWebDocumentView touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self.uiScroller touchesMoved:touches withEvent:event];
[self.uiWebDocumentView touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.uiScroller touchesEnded:touches withEvent:event];
[self.uiWebDocumentView touchesEnded:touches withEvent:event];
[self.superview didFinishGesturesInView:self.superview forEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self.uiScroller touchesCancelled:touches withEvent:event];
[self.uiWebDocumentView touchesCancelled:touches withEvent:event];
[self.nextResponder touchesCancelled:touches withEvent:event];
}
@end
@implementation TouchUIWebView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// Initialization code
DummyTapView* view = [[DummyTapView alloc]
initWithFrame:CGRectMake( 0,0,self.frame.size.width,self.frame.size.height)];
[self addSubview:view];
[view release];
}
return self;
}
@end
Power by