- (void)viewDidLoad{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
self.webView.delegate=self;
NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"test.html"];
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSString *newHTMLString=[htmlstring stringByAppendingString:@"<script language=/"javascript/">document.ontouchstart=function(){ document.location=/"myweb:touch:start/"; }; document.ontouchend=function(){ document.location=/"myweb:touch:end/"; }; document.ontouchmove=function(){ document.location=/"myweb:touch:move/"; } </script>"];
[self.webView loadHTMLString:newHTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[htmlstring release];
webView.backgroundColor = [UIColor redColor];
webView.frame = CGRectMake(0, 0, 768, 1024);
[self.view addSubview:webView];
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"myweb"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
{
NSLog(@"%@",[components objectAtIndex:2]);
}
return NO;
}
return YES;
}
UIScrollView上touch事件的捕捉:
创建一个继承于UIScrollView的类,
@interface MyScrollView : UIScrollView {
}
@end
并改写其touch方法;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(!self.dragging)
{
[[self nextResponder] touchesBegan:touches withEvent:event];
}
[super touchesBegan:touches withEvent:event];
//NSLog(@"MyScrollView touch Began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!self.dragging)
{
[[self nextResponder] touchesEnded:touches withEvent:event];
}
[super touchesEnded:touches withEvent:event];
}