在如今html5越来越横行的年代,app里面webview的交互显得越发重要起来。。。
有时候你会碰到内置控件与webview共存在一个页面的情况。如下图:
这个时候令人蛋疼的事儿就来啦。。 你如果不加控制。让webview只在下半部分显示。在6和6p上还好,但是在4s上就不能忍啦。
你必须控制上面轮播的与webview同时移动到顶端。然后webview自主滑动。。这才是人家产品要的效果!!!
要想实现上述效果。监听到webview的滑动是前提!。网上搜了半天。没有什么有效地答案。
苦思冥想了半天。。终于解决啦
啥都不说啦。上代码!!!!:::::
监听到webview的滑动: _webView.scrollView.delegate = self;
实现随动效果:
static float tempY = 0;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
tempY += scrollView.contentOffset.y;
if (tempY >= 150) {
tempY = 150;
}
if (tempY <=0) {
tempY =0 ;
}
NSLog(@"tempY = %f", tempY);
if (tempY < 150&&tempY>0) {//根据当前temp来判断轮播图的状态
scrollView.contentOffset = CGPointZero;
//滑动的距离
CGFloat scrolly = tempY;
//向上滑动的距离越大。透明度越小
CGFloat h = 150 - scrolly;//头部高度减去移动距离
// CGFloat alpha = h/150.0;
// NSLog(@"y=%f, alpha=%f", scrolly,alpha);
// sdCycleView.alpha = alpha>1?1.0:alpha;
// NSLog(@"h=========&********%f",h);
sdCycleView.frame = CGRectMake(0, 0, Width, h);
_webView.frame = CGRectMake(0, sdCycleView.bottom, Width, Height-113);
}else if(tempY == 0){//轮播图显现之后重置
sdCycleView.frame = CGRectMake(0, 0, Width, 150 - scrollView.contentOffset.y);
_webView.frame = CGRectMake(0, sdCycleView.bottom, Width, Height-113);
}else {//轮播图隐藏之后webview正常滑动
sdCycleView.frame = CGRectMake(0, 0, Width, 0);
_webView.frame = CGRectMake(0, 0, Width, Height-113);
}
}
有同样需求的同学可以参考一下。!!