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];
}
- (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];
}
相关文章推荐
- UIWebView和UIScrollView上接受touch事件
- UIWebView和UIScrollView上接受touch事件
- 细说UIScrollView上的Touch 事件
- 如何让UIScrollView里面的UIImageView响应touch事件
- 解决UIScrollView截获touch事件的一个极其简单有效的办法
- UIScrollView不能响应touch事件的解决办法
- UIScrollView无法响应touch事件的解决办法
- IOS开发:UIScrollView类介绍及如何简单地截获touch事件
- uiscrollview的touch事件 以及上面子视图touch事件 实现原理
- UIScrollView的作用原理,实现scrollView传递touch事件给子视图
- 『IOS』UIScrollView无法响应touch事件的解决办法
- 细说UIScrollView上的Touch 事件 推荐
- UIScrollView不能响应touch事件的解决办法
- UIScrollView无法响应touch事件的解决办法
- 细说UIScrollView上的Touch 事件
- 关于UIScrollview不能响应touch事件的解决办法
- 解决UIScrollView,UIImageView等控件不能响应touch事件的问题
- UIScrollView无法响应touch事件的解决办法
- 解决UIScrollView,UIImageView等控件不能响应touch事件的问题
- 细说UIScrollView上的Touch 事件