因为项目当中涉及webView与scrollView的滑动交互,一开始是经过计算webView的高度而且禁用webView的滑动,而后改变scrollView的contentSize去实现滑动操做,但这种作法当webView加载的内容里图片过多时会形成内存爆满,因此换了一种作法去实现。git
Demo下载github
响应多手势的方法web
/**
让ScrollView响应多手势
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
复制代码
/** scrollView是否可滑动 */
@property (nonatomic, assign) BOOL isCanScroll;
/** scrollV */
@property (nonatomic, strong) LQScrollView *scrollV;
复制代码
/** 是否能够滑动 */
@property (nonatomic, assign) BOOL isWebCanScroll;
复制代码
经过isWebCanScroll属性来控制是否可滑动,通用设置webView中ScrollView的contentOffset来实现webView不可滑动,当webView可滑动而且滑动到顶部的时候,此时改变webView的isWebCanScroll属性从新让webView不可滑,而且将这种状态用通知发送出去。bash
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.scrollView) {
CGFloat offY = scrollView.contentOffset.y;
// NSLog(@"offY == %lf",offY);
if (!self.isWebCanScroll) { //经过设置 contentOffset 让webView不可滑动
self.scrollView.contentOffset = CGPointZero;
}
if (offY < 0) { //当webView滑动顶部时 使webView不可滑 而且将该状态用通知发送出去
self.isWebCanScroll = NO;
self.scrollView.contentOffset = CGPointZero;
[[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEWSCROLLTOTOP" object:nil];
}
}
}
复制代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offY = scrollView.contentOffset.y;
if (scrollView == self.scrollV) {
if (offY >= TopViewHeight) { //此时webView到达顶部 让scrollView不可滑 让webView可滑
self.scrollV.contentOffset = CGPointMake(0, TopViewHeight);
if (self.isCanScroll) {
self.isCanScroll = NO;
self.webV.isWebCanScroll = YES;
}
}else if (offY >= 0 && offY < 200){ //scrollView处于可滑动范围
if (self.isCanScroll) {
self.scrollV.contentOffset = CGPointMake(0, offY);
}else{
if (self.webV.isWebCanScroll && self.webV.scrollView.contentOffset.y == 0) { //解决临界值问题
self.isCanScroll = YES;
self.webV.isWebCanScroll = NO;
} else {
self.scrollV.contentOffset = CGPointMake(0, TopViewHeight);
}
}
}
}
}
复制代码
/**
接收webView不可滑的通知 scrollView设置为可滑
*/
- (void)webViewScrollToTop:(NSNotification *)nofi{
self.isCanScroll = YES;
}
复制代码
补充说明:post
该处理方式不只适用于webView与scrollView也适用于tableView于ScrollView的交互。处理方式是同样的,都是经过BOOL 属性去控制是否可滑动,而是否可滑动的处理是经过在ScrollView的代理方法scrollViewDidScroll去控制ScrollView的contentOffset实现的。ui
GitHub地址 :github.com/qiuyubude/G…atom
简书地址:www.jianshu.com/p/5ee0c074a…spa