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

iOS开发-UIWebView

2013-10-29 22:23 381 查看
设置UIWebView透明

[cpp] view
plaincopyprint?

[webview setBackgroundColor:[UIColor clearColor]];

webview.opaque = NO;

禁止UIWebView滚动

[cpp] view
plaincopyprint?

webView.scrollView.bounces = NO; //__IPHONE_5_0

[cpp] view
plaincopyprint?

UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];

scrollView.bounces = NO;

获取UIWebView高度

[cpp] view
plaincopyprint?

- (void)webViewDidFinishLoad:(UIWebView *)webView1

{

UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];

CGFloat webViewHeight = [scrollView contentSize].height;

NSString *curHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

CGRect newFrame = webView.frame;

newFrame.size.height = webViewHeight;

webView.frame = newFrame;

}

使用JS给UIWebView添加事件响应

1.首先定义事件的JavaScript

[cpp] view
plaincopyprint?

// timeStamp 微秒

static NSString * const webTouchJavaScriptString =

@"<script language=\"javascript\">document.ontouchstart=function(event){\

x=event.targetTouches[0].clientX;\

y=event.targetTouches[0].clientY;\

time=event.timeStamp;\

document.location=\"wiweb:touch:start:\"+x+\":\"+y+\":\"+time;};\

document.ontouchmove=function(event){\

x=event.targetTouches[0].clientX;\

y=event.targetTouches[0].clientY;\

document.location=\"wiweb:touch:move:\"+x+\":\"+y;};\

document.ontouchcancel=function(event){\

document.location=\"wiweb:touch:cancel\";};\

document.ontouchend=function(event){\

time=event.timeStamp;\

document.location=\"wiweb:touch:end:\"+time;}; </script>";

2.组织字符串

[cpp] view
plaincopyprint?

NSString *webviewText = @"<style>body{margin:0;background-color:transparent;color:#000000;word-wrap:break-word;word-break:break-all;font:18px/22px system}</style>";

NSString *htmlString = [webviewText stringByAppendingFormat:@"%@", @"自定SDFSDFSDFSDF义字体fsdgjdlagj asdkgjksdh卡号给卡仕达;逛了会街啊啊流口水 http://www.baidu.com 的感觉卡拉;四大金刚;拉开始打工绿卡;但是结果来看;就爱上的看过就卡的;上来讲赶快来;啊都是经过后ihgoiadsg;肯定是噶上的好;拉克丝的价格爱国阿斯顿改了可"];

NSString *newHTMLString=[htmlString stringByAppendingString:webTouchJavaScriptString];

3.事件响应

[cpp] view
plaincopyprint?

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

BOOL result = YES;

NSURL *requestURL =[[request URL] retain];

NSString *requestString = [[request URL] absoluteString];

static BOOL bstart = NO;

static BOOL bmove = NO;

static double lasttime = 0;

NSString *str = [requestURL scheme];

if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"])

&& (navigationType == UIWebViewNavigationTypeLinkClicked) )

{

result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]];

}

else

{

[requestURL release];

NSArray *components = [requestString componentsSeparatedByString:@":"];

if ([components count] > 2

&& [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"]

&& [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])

{

NSString *eventString=[components objectAtIndex:2];

if ([eventString isEqualToString:@"start"])

{

float pointX=[[components objectAtIndex:3] floatValue];

float pointY=[[components objectAtIndex:4] floatValue];

double time=[[components objectAtIndex:5] doubleValue];

CGPoint aPoint = CGPointMake(pointX, pointY);

NSLog(@"start: %@", NSStringFromCGPoint(aPoint));

NSLog(@"start time: %0f interval: %0f", time/1000, (time - lasttime)/1000);

lasttime = time;

bstart = YES;

bmove = NO;

NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);

}

else if ([eventString isEqualToString:@"move"])

{

float pointX=[[components objectAtIndex:3] floatValue];

float pointY=[[components objectAtIndex:4] floatValue];

CGPoint aPoint=CGPointMake(pointX, pointY);

NSLog(@"move: %@", NSStringFromCGPoint(aPoint));

bmove = YES;

NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);

}

else if ([eventString isEqualToString:@"cancel"])

{

NSLog(@"cancel");

bstart = NO;

bmove = NO;

NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);

}

else if ([eventString isEqualToString:@"end"])

{

double time=[[components objectAtIndex:3] doubleValue];

NSLog(@"end");

NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);

NSLog(@"end time: %0f interval: %0f", time/1000, (time - lasttime)/1000);

if (bstart && !bmove)

{

if (time - lasttime > 400)

{

NSLog(@"LongPress!!!!!!");

}

else

{

NSLog(@"Click!!!!!!");

}

}

bstart = NO;

bmove = NO;

}

return NO;

}

}

NSURL *url = [request URL];

NSString *curUrl= [url absoluteString];

NSLog(@"cururl: %@", curUrl);

return result;

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