如何正确使用 swipeWithEvent 导航 webView,Obj-C [英] How to properly use swipeWithEvent to navigate a webView, Obj-C

查看:19
本文介绍了如何正确使用 swipeWithEvent 导航 webView,Obj-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用事件 swipeWithEvent 来实现在我的 webView 中向后和第四次导航的功能.但是,我不知道我应该如何使用它.

I want to implement the ability to navigate back and fourth in my webView by using the Event swipeWithEvent. However, I have no idea how I am suppose to use this.

我有一个主要的 webView,以及向后和第四个导航的两种方法.这里的一个大问题是我不确定如何写这个问题.我只需要知道如何识别我的 webView 上的滑动手势并调用我的两个方法.类似于 Safari、Google Chrome 和 Mozilla Firefox 所做的事情.谢谢.

I have one main webView, and two methods that navigate back and fourth. A big problem here is that I'm not exactly sure how to write this question. I just need to know how to recognize swipe gestures on my webView and call my two methods. Similar to what Safari, Google Chrome, and Mozilla Firefox do. Thanks.

编辑我已经实现了这些方法,这些方法可以让我前后滑动.

EDIT I have implemented these methods which are suppose to allow me to swipe back and forward.

- (void)swipeWithEvent:(NSEvent *)event {
    NSLog(@"Swipe With Event");
    CGFloat x = [event deltaX];
    //CGFloat y = [event deltaY];

    if (x != 0) {
        (x > 0) ? [self goBack:self] : [self goForward:self];
    }
}


-(BOOL)recognizeTwoFingerGestures
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:@"AppleEnableSwipeNavigateWithScrolls"];
}

- (void)beginGestureWithEvent:(NSEvent *)event
{
    if (![self recognizeTwoFingerGestures])
        return;

    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:nil];

    self.twoFingersTouches = [[NSMutableDictionary alloc] init];

    for (NSTouch *touch in touches) {
        [twoFingersTouches setObject:touch forKey:touch.identity];
    }
}

- (void)endGestureWithEvent:(NSEvent *)event
{
    if (!twoFingersTouches) return;

    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:nil];

    // release twoFingersTouches early
    NSMutableDictionary *beginTouches = [twoFingersTouches copy];
    self.twoFingersTouches = nil;

    NSMutableArray *magnitudes = [[NSMutableArray alloc] init];

    for (NSTouch *touch in touches)
    {
        NSTouch *beginTouch = [beginTouches objectForKey:touch.identity];

        if (!beginTouch) continue;

        float magnitude = touch.normalizedPosition.x - beginTouch.normalizedPosition.x;
        [magnitudes addObject:[NSNumber numberWithFloat:magnitude]];
    }

    // Need at least two points
    if ([magnitudes count] < 2) return;

    float sum = 0;

    for (NSNumber *magnitude in magnitudes)
        sum += [magnitude floatValue];

    // Handle natural direction in Lion
    BOOL naturalDirectionEnabled = [[[NSUserDefaults standardUserDefaults] valueForKey:@"com.apple.swipescrolldirection"] boolValue];

    if (naturalDirectionEnabled)
        sum *= -1;

    // See if absolute sum is long enough to be considered a complete gesture
    float absoluteSum = fabsf(sum);

    if (absoluteSum < kSwipeMinimumLength) return;

    // Handle the actual swipe
    if (sum > 0)
    {
        [self goForward:self];
    } else
    {
        [self goBack:self];
    }


}

但是,这段代码没有做任何事情.它似乎根本没有被调用.

However, this code isn't doing anything. It doesn't appear to be getting called at all.

推荐答案

对于现代操作系统(Lion 和更新的操作系统),这是我如何用 Y 做 X?"的问题之一.答案是不要使用 Y"的问题.

For modern OSes (Lion and newer), this is one of those "How do I do X with Y?" questions where the answer is "Don't use Y."

-swipeWithEvent: 用于实现 10.6 风格的触控板滑动,屏幕上的内容不会随着滑动而移动.大多数 Mac 触控板不再配置为允许这种滑动;触控板偏好面板中的在页面之间滑动"首选项必须设置为用三个手指滑动"才能使用,这既不是默认设置,也不是用户可以更改的常用设置.

-swipeWithEvent: is used to implement 10.6-style trackpad swiping, where the content on the screen doesn't move with the swipe. Most Mac trackpads are not configured to allow this kind of swiping anymore; the "Swipe between pages" preference in Trackpad pref pane has to be set to "swipe with three fingers" for it to be available, and that's neither the default setting nor a common setting for users to change.

Lion 风格的流畅滑动"作为滚动事件出现.Xcode SDK 中的 PictureSwiper 示例项目是一个很好的起点,但作为概述,您可以这样做:

Lion-style "fluid swipes" instead come in as scroll events. The PictureSwiper sample project in your Xcode SDK is a good place to start, but as an overview, here's what you do:

如果只需要支持10.8+

在历史堆栈模式下使用 NSPageController.

Use an NSPageController in history stack mode.

如果需要支持 10.7

  1. 认真考虑仅支持 10.8+,至少对于此功能.手动实现它是一个巨大的混乱.别说我没有警告你.

  1. Seriously consider supporting only 10.8+, at least for this feature. Implementing it manually is a huge mess. Don't say I didn't warn you.

创建一个自定义视图,它是您希望可滑动的任何视图的超级视图.

Create a custom view that is a superview to whatever views you want to be swipeable.

覆盖 -wantsScrollEventsForSwipeTrackingOnAxis: 以返回相应轴的 YES.如果您正在执行 Safari 风格的后退/前进导航,那就是 NSEventGestureAxisHorizo​​ntal.

Override -wantsScrollEventsForSwipeTrackingOnAxis: to return YES for the appropriate axis. If you're doing Safari-style back/forward navigation, that's NSEventGestureAxisHorizontal.

深呼吸;这个太棒了.

在您的自定义视图中覆盖 -scrollWheel:.您的覆盖应该调用 -trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:.粗略地说,衰减量阈值最小值是用户可以向左滑动的数据浏览量,最大值是他们可以向右滑动的数据量.处理程序是一个在用户滑动时重复调用的块;它应该:

Override -scrollWheel: in your custom view. Your override should call -trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:. Roughly, the dampen amount threshold minimum is how many viewfuls of data the user can swipe to the left, and the maximum is how many they can swipe to the right. The handler is a block that's called repeatedly as the user swipes; it should:

  1. 在内容视图后面放置一个 NSImageView,其中包含您要返回/前进到的页面的屏幕截图.
  2. 移动内容视图以匹配用户的动作.请注意,gestureAmount 参数与阻尼量阈值一样,是项目的(小数,可能是负数)数量;您必须将其乘以视图宽度才能正确定位内容视图.
  3. 如果手势阶段是 NSEventPhaseEnded,则评估 gestureAmount 以确定用户是否完成了手势.如果他们没有,将内容视图动画化回原位;如果他们这样做了,请将内容视图放回原位,不使用动画并更新它以匹配屏幕截图.
  1. Place an NSImageView with a screenshot of the page you're going back/forward to behind your content view.
  2. Move the content view to match the user's movements. Note that the gestureAmount parameter is, like the dampen amount thresholds, a (fractional and possibly negative) number of items; you have to multiply it by the view width to correctly position the content view.
  3. If the gesture phase is NSEventPhaseEnded, evaluate the gestureAmount to determine if the user completed the gesture. If they didn't, animate the content view back into place; if they did, put the content view back in place with no animation and update it to match the screenshot.

如您所见,实际实现处理程序非常复杂,我什至没有描述所有细节.即使有了所有这些细节,一个技术高超的程序员也可能需要花几天时间才能做到这一点.10.7 SDK 中的 PictureSwiper 示例是一个很好的起点.(PictureSwiper 10.8 版本使用 NSPageController.就像你应该做的那样.)

As you can see, actually implementing the handler is very involved, and I haven't even described all of the specifics. Even with all of these details, a highly skilled programmer will probably have to spend a few days getting this just right. The PictureSwiper sample in the 10.7 SDK is a good place to start. (The 10.8 version of PictureSwiper uses NSPageController. Just like you ought to do.)

这篇关于如何正确使用 swipeWithEvent 导航 webView,Obj-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆