滑动html页面如果我水平滑动,请加载另一个html页面

时间:2012-02-18 07:09:08

标签: ipad mobile navigation swipe

如何滑动(左)html页面?

1 个答案:

答案 0 :(得分:0)

处理在Web视图上发生的手势非常棘手,因为UIWebView真的很贪婪,并希望处理所有这些。

与网络视图一样,您可以尝试使用Javascript完成所有操作。您可以在此S.O. post中阅读有关它的内容。

我喜欢在网页视图上处理捏合(缩放)和滑动的方式如下:

  1. 子类UIWindow

       @interface MyWebNavigatorWindow : UIWindow {
    
  2. application:didFinishLaunchingWithOptions中安装该窗口类型的实例作为您应用的窗口:

      _window = [[MyWebNavigatorWindow alloc] initWithFrame:rect];
    

    或者,您可以在Interface Builder中为窗口对象分配一个类。

  3. 处理sendEvent课程MyWebNavigatorWindow中的滑动和捏合:

      - (void)sendEvent:(UIEvent*)event {
        NSSet* allTouches = [event allTouches];
        UITouch* touch = [allTouches anyObject];
        UIView* touchView = [touch view];
    
  4. 您需要sendEvent中的机制来检测何时在您的网络视图中发生触摸。在我的情况下,我“注册”我要处理触摸的所有视图,然后检查触摸是否在其中:

            for (UIView* view in self.controlledViews) {  // self.controlledViews is the array of all "registered" views
          if ([touchView isDescendantOfView:view]) {
    
  5. 然后我处理各种触摸阶段以检测它是什么样的手势(代码片段不可编辑,但它给出了想法):

            if (touch.phase == UITouchPhaseBegan) {
            NSLog(@"TOUCH BEGAN");
            _initialView = touchView;
            startTouchPosition1 = [touch locationInView:self];
            startTouchTime = touch.timestamp;
    
            if ([allTouches count] > 1) {
                startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self];
                previousTouchPosition1 = startTouchPosition1;
                previousTouchPosition2 = startTouchPosition2;
            }
        }
    
        if (touch.phase == UITouchPhaseMoved) {
            NSLog(@"TOUCH MOVED");
            if ([allTouches count] > 1) {
                CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self];
                CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self];
    
                CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2);
                CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2);
                if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) {
                    NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance];
                    if (currentFingerDistance > previousFingerDistance) {
                        NSLog(@"zoom in");
                        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance];
                    } else {
                        NSLog(@"zoom out");
                        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance];
                    }
                }
            }
        }
    
        if (touch.phase == UITouchPhaseEnded) {
            CGPoint currentTouchPosition = [touch locationInView:self];
    
            // Check if it's a swipe
            if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN &&
                fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) &&
                touch.timestamp - startTouchTime < 0.7
                ) {
                // It appears to be a swipe.
                if (startTouchPosition1.x < currentTouchPosition.x) {
                    NSLog(@"swipe right");
                    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:touch];
                } else {
                    NSLog(@"swipe left");
                    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:touch];
                }
            }
            startTouchPosition1 = CGPointMake(-1, -1);
            _initialView = nil;
        }