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

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

本文介绍了滑动html页面如果我水平滑动则加载另一个html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么可能(向左)滑动html页面?

How would it be possible to swipe (left) html pages?

推荐答案

处理在Web视图上发生的手势非常棘手,因为UIWebView确实对触摸很贪婪,并且希望处理所有手势.

Handling gestures happening on a web view is tricky, since UIWebView is really greedy with touches and wants to handle them all.

和通常的网络视图一样,您可以尝试使用Javascript做所有事情.您可以在 S.O中阅读有关此内容的内容.发布.

As usual with web views, you could try and do everything with Javascript. You can read something about it in this S.O. post.

我更喜欢以下方法:在网络视图上进行缩放(缩放)和滑动操作:

The way I prefer handling pinching (zoom) and swiping on a web view though is following:

  1. 子类:

  1. subclass UIWindow:

   @interface MyWebNavigatorWindow : UIWindow {

  • application:didFinishLaunchingWithOptions中安装该窗口类型的实例作为您的应用程序的窗口:

  • install an instance of that window type as the window for your app in application:didFinishLaunchingWithOptions:

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

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

    Alternatively, you can assign a class to your window object in Interface Builder.

    处理MyWebNavigatorWindow类中sendEvent中的滑动和捏住:

    handle swipes and pinches in sendEvent in your MyWebNavigatorWindow class:

      - (void)sendEvent:(UIEvent*)event {
        NSSet* allTouches = [event allTouches];
        UITouch* touch = [allTouches anyObject];
        UIView* touchView = [touch view];
    

  • 您将需要sendEvent中的一种机制来检测何时在Web视图内发生触摸.就我而言,我注册"了要处理触摸的所有视图,然后检查触摸是否在其中:

  • You will need a mechanism in sendEvent to detect when the touch happens inside of your web view. In my case, I "register" all the views that I want to handle touch for and then check if the touch is inside them:

            for (UIView* view in self.controlledViews) {  // self.controlledViews is the array of all "registered" views
          if ([touchView isDescendantOfView:view]) {
    

  • 然后我处理各种触摸阶段,以检测它是什么样的手势(代码段无法编译,但可以说明问题):

  • then I handle the various touch phases to detect what kind of gesture it is (code snippet not compilable, but it gives the idea):

            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;
        }
    

  • 这篇关于滑动html页面如果我水平滑动则加载另一个html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-27 02:49