【问题标题】:Two finger swipe in Yosemite 10.10优胜美地 10.10 中的两指轻扫
【发布时间】:2020-11-10 13:49:24
【问题描述】:

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

https://github.com/oscardelben/CocoaNavigationGestures

要在 Mac 上捕获两次手指滑动,在 Yosemite 下它不再起作用。任何人都知道发生了什么变化,或者我需要改变什么才能使其正常工作。

【问题讨论】:

标签: objective-c macos cocoa osx-yosemite


【解决方案1】:

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

【讨论】:

  • 这适用于一个手指,你如何让两个手指来处理这个?
【解决方案2】:

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

    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")
        }
    }

【讨论】:

    【解决方案3】:

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

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

    希望我能帮上忙。

    【讨论】:

    • 他不是在问如何设置用户偏好,而是在谈论在 Cocoa 应用程序中运行时检测它。
    • 但据我所知,系统偏好设置中默认禁用它。
    • 不是系统偏好问题。问题是代码不再适用于 NSView。
    【解决方案4】:

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

    #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");
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2015-06-02
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多