两只手指在优胜美地10.10中滑动

时间:2014-10-26 06:51:24

标签: objective-c macos cocoa osx-yosemite

我一直在使用类似的方法:

https://github.com/oscardelben/CocoaNavigationGestures

要在Mac上捕获两个手指滑动,在Yosemite下它不再有效。任何人都知道发生了什么变化,或者我需要改变什么才能发挥作用。

4 个答案:

答案 0 :(得分:1)

接受的答案对我来说效果不好 - 通常无法检测到滑动。相反,我覆盖wantsScrollEventsForSwipeTrackingOnAxis:(NSEventGestureAxis)axis以返回适当轴的YES,然后覆盖scrollWheel:(NSEvent *)theEvent以检测滚动。每次都很完美。

答案 1 :(得分:0)

在Mac上,转到系统偏好设置,您会找到触控板的所有不同设置。

从这里开始:https://discussions.apple.com/thread/5710582

希望我帮助过。

答案 2 :(得分:0)

这是我的解决方案,似乎对我有用。

#define kSwipeMinimumLength 0.2
- (void)touchesBeganWithEvent:(NSEvent *)event{
    if(event.type == NSEventTypeGesture){
        NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
        if(touches.count == 2){
            self.twoFingersTouches = [[NSMutableDictionary alloc] init];

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


- (void)touchesMovedWithEvent:(NSEvent*)event {
    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:self];
    if(touches.count > 0){
        NSMutableDictionary *beginTouches = [self.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]];
        }

        float sum = 0;

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

        // 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
        // This might need to be > (i am using flipped coordinates), you can use an else to go forward also.
        if (sum > 0){
            NSLog(@"go back");
        }
    }
}

答案 3 :(得分:0)

基于@bmuller的Swift 5.3的完整答案是:

    override func wantsScrollEventsForSwipeTracking(on axis: NSEvent.GestureAxis) -> Bool {
        return axis == .horizontal
    }
    
    override func scrollWheel(with event: NSEvent) {
        if event.scrollingDeltaX < 0 {
            print("Go forward")
        }
        else {
            print("Go back")
        }
    }

使用此代码,可以一次手势发送多次滑动事件。您可能需要将阶段(NSEventPhase)处理代码添加到scrollWheel(...)函数中,例如

     override func scrollWheel(with event: NSEvent) {

        guard event.phase == .began else {
            return
        }

        if event.scrollingDeltaX < 0 {
            print("Go forward")
        }
        else {
            print("Go back")
        }
    }