您的位置:首页 > 移动开发

UIWebView和UIScrollView上接受touch事件

2010-08-19 09:33 239 查看
UIWebView 的touch事件的捕捉:

- (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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: