I have an application where I have a webview in which I load an HTML page. The html page which I am loading contains 5 pages inside it. Actually when I scroll the webview the scrollViewDidScroll:
delegate method of my webview is called where I am checking the offset to get the direction in which the webview is scrolling.
When I scroll the webview to the right I get content offset as 2
and it moves to the next page of html. When I scroll the webview again to the right it shows the content offset as 0
and moves to previous page.
When the direction is right/left I want to call a JavaScript method on webview.
This is my code:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"self.webview.scrollView.contentOffset.x:%f",self.webview.scrollView.contentOffset.x);
NSLog(@"%d",lastContentOffset);
NSString *val = @"getPageNo()";
NSString *val1= [webview stringByEvaluatingJavaScriptFromString:val];
int pageno = [val1 intValue];
NSLog(@"%@",val1);
if ( self.webview.scrollView.contentOffset.x==0)
{
//this means webview moves in left direction
NSLog(@"%f",self.webview.scrollView.contentOffset.x);
NSLog(@"%d",lastContentOffset);
NSString *showpageno = [NSString stringWithFormat:@"showPage('%d')",pageno-1];
[webview stringByEvaluatingJavaScriptFromString:showpageno];
}
else if ( self.webview.scrollView.contentOffset.x>0)
{
//this means webview moves in right direction
NSLog(@"%f",self.webview.scrollView.contentOffset.x);
NSLog(@"%d",lastContentOffset);
NSString *showpageno = [NSString stringWithFormat:@"showPage('%d')",pageno+1];
[webview stringByEvaluatingJavaScriptFromString:showpageno];
}
}