1、在單層web頁面上經(jīng)常使用如下的方法判斷上下滑動
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
? ? if(scrollView ==self.tableview)
? ? {
? ? ? ? if (self.tableview.contentOffset.y > _oldY)
? ? ? ? {
? ? ? ? ? ? //向上滑動
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:CCWNewsScrollUp object:nil];
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //向下滑動
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:CCWNewsScrollDown object:nil];
? ? ? ? }
? ? ? ? if (self.tableview.contentOffset.y == 0)
? ? ? ? {
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:CCWNewsScrollToTop object:nil];
? ? ? ? }
? ? }
}
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{
? ? // 獲取開始拖拽時tableview偏移量
? ? self.oldY = self.tableview.contentOffset.y;
}
2、但是當web頁面時多層嵌套的時候君账,使用上面的方法就會失效,只有在滑動到最底部繼續(xù)上滑才會觸發(fā)上面的方法。這個時候就要使用其他的方法 迹鹅,如監(jiān)控手指的滑動
(1)先在web頁面添加滑動手勢
? ? //添加手勢監(jiān)聽上下滑動
? ? UIPanGestureRecognizer *apan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(goPanNext:)];
? ? apan.delegate=self;
? ? apan.maximumNumberOfTouches=1;
? ? apan.minimumNumberOfTouches=1;
? ? [_webView addGestureRecognizer:apan];
(2)實現(xiàn)滑動手勢的方法
- (void)goPanNext:(UIPanGestureRecognizer*)sender
{
? ? if (sender.state == UIGestureRecognizerStateChanged)?
? ? {
? ? ? ? [self commitTranslation:[sender translationInView:self]];
? ? }
}
(3)下面是監(jiān)控手勢上下左右滑動的方法
/** 判斷手勢方向? */
- (void)commitTranslation:(CGPoint)translation
{
? ? CGFloatabsX =fabs(translation.x);
? ? CGFloatabsY =fabs(translation.y);
? ? // 設(shè)置滑動有效距離
? ? if(MAX(absX, absY) < 10)
? ? ? ? return;
? ? if(absX > absY )
? ? {
? ? ? ? if(translation.x<0)
? ? ? ? {
? ? ? ? ? ? //向左滑動
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //向右滑動
? ? ? ? }
? ? }
? ? elseif(absY > absX)
? ? {
? ? ? ? if(translation.y<0)
? ? ? ? {
? ? ? ? ? ? //向上滑動
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:CCWNewsScrollUp object:nil];
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //向下滑動
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:CCWNewsScrollDown object:nil];
? ? ? ? }
? ? }
}