我如何在 objectiveC OSX 中禁用带有 NSPageController 的 NSTableView 的滚动事件?

How do i disable scroll events for a NSTableView with a NSPageController in objectiveC OSX?

我有一个 NSPageController - 允许用户在视图之间水平滑动的控制器。

问题是我们滑动到的视图中有一个 NSTableView。 那时我无法滑动回到之前的视图,因为 NSTableView 是 "eating" 所有滑动手势。有谁知道如何禁用 only NSTableView 上的水平滚动,以便水平滑动 gesters 会转到 NSPageController 的视图而不是 table?

试试这个

- (void)scrollWheel:(NSEvent *)theEvent
{
    [self.nextResponder scrollWheel:theEvent];
}

希望对您有所帮助。

创建一个布尔值,允许您的 panGesture 在 panScrollView 内操作。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.blockPanGesture = YES;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:
    (BOOL)decelerate
{
self.blockPanGesture = NO;
}

然后

-(void)panScrollView:(UIPanGestureRecognizer *)panGestureRecognizer
{
  if(self.blockPanGesture == NO)
   {
   // do the stuff
   }
}

如果您亲自平移整个 tableview,我会将手势识别器放在 tableView 本身上...否则还有

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

// Return YES if you want the specified item to be editable.
return YES;
}

它会为您处理所有这些...但我假设您不想使用它是有原因的。你也可以建立在这个逻辑上,例如你可能想调整滚动视图何时可以拖动,通过在 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 等中进行类似的检查。

在 Swift 中解决了 NSScrollView

的以下自定义 class 实现
class HorizontalScrollDisabledTable: NSScrollView {
    var currentScrollIsHorizontal: Bool = true;

    override func scrollWheel(theEvent: NSEvent) {

        var phase: NSEventPhase = theEvent.phase

        /* Ensure that both scrollbars are flashed when the user taps trackpad with two fingers */
        if (phase == .MayBegin) {
            super.scrollWheel(theEvent) ;
            self.nextResponder?.scrollWheel(theEvent);
            return;
        }
        /* Check the scroll direction only at the beginning of a gesture for modern scrolling devices */
        /* Check every event for legacy scrolling devices */
        if (phase == .Began || (phase == .None && theEvent.momentumPhase == .None)) {
            currentScrollIsHorizontal = fabs(theEvent.scrollingDeltaX) > fabs(theEvent.scrollingDeltaY);
        }
        if (!currentScrollIsHorizontal ) {
            super.scrollWheel(theEvent);
        } else {
            self.nextResponder!.scrollWheel(theEvent);
        }
    }
}

这是上面 scrollWheel 方法的相同答案的代码,但在 ObjectiveC 中。

-(void)scrollWheel:(NSEvent *)theEvent {

    NSEventPhase phase = [theEvent phase];

    /* Ensure that both scrollbars are flashed when the user taps trackpad with two fingers */
    if (phase == NSEventPhaseMayBegin) {
        [super scrollWheel:theEvent];
        [self.nextResponder scrollWheel:theEvent];
            return;
    }
    /* Check the scroll direction only at the beginning of a gesture for modern scrolling devices */
    /* Check every event for legacy scrolling devices */
    if (phase == NSEventPhaseBegan || (phase == NSEventPhaseNone && theEvent.momentumPhase == NSEventPhaseNone)) {
        currentScrollIsHorizontal = fabs(theEvent.scrollingDeltaX) > fabs(theEvent.scrollingDeltaY);
    }
    if (!currentScrollIsHorizontal ) {
        [super scrollWheel:theEvent];
    } else {
        [self.nextResponder scrollWheel:theEvent];
    }
}