NSView禁用平滑滚动

时间:2016-01-18 15:24:54

标签: macos cocoa scroll nsview nsresponder

我有一个 MTKView ,我在这里渲染一个三角形,我希望能够使用调用 scrollWheel的双指拖动手势来翻译它:方法。 scrollWheel:方法的默认行为是生成额外的滚动事件,以实现随着时间推移而消失的平滑滚动。我想禁用这种行为,并且只要我将手指从触控板上抬起,就会有一个严格的滚动。有谁知道如何禁用 NSView 的平滑滚动?

2 个答案:

答案 0 :(得分:1)

您可以检查momentumPhase参数的NSEvent属性,以确定是否忽略该事件。

答案 1 :(得分:0)

是的,检查momentumPhase很重要...另外,我添加了一个成员var isTouchEventActive来检测手势。

另外,通过忽略总x + y + z delta为0来防止跳跃!

- (void)touchesBeganWithEvent:(NSEvent *)event { isTouchEventActive = YES; }
- (void)touchesMovedWithEvent:(NSEvent *)event { isTouchEventActive = YES; }
- (void)touchesEndedWithEvent:(NSEvent *)event { isTouchEventActive = NO; }
- (void)touchesCancelledWithEvent:(NSEvent *)event { isTouchEventActive = NO; }

- (void)beginGestureWithEvent:(NSEvent *)event { isTouchEventActive = YES; }
- (void)endGestureWithEvent:(NSEvent *)event { isTouchEventActive = NO; }

- (void)magnifyWithEvent:(NSEvent *)event
{
    double totalDelta = [event magnification];
    if ( totalDelta == 0 )
        return; // i think this is the indication that a trackpad gesture has 'finished'... on the iphone, i think it means 'stop animate scroll now'

    allowAnimate = NO;
    [self zoom:totalDelta isSlider:NO isAbsolute:NO useMousePt:YES];
}

- (void)scrollWheel:(NSEvent *)event {
    NSUInteger flags = [event modifierFlags];

    NSRect visRect = [self visibleRect];
    CGFloat deltaX = [event deltaX], deltaY = [event deltaY], deltaZ = [event deltaZ];

    double totalDelta = deltaX + deltaY + deltaZ;
    //printf( "deltaX %f deltaY %f deltaZ %f totalDelta %f", deltaX, deltaY, deltaZ, totalDelta );
    //NSLog( @"%@", [event description] );
    if ( totalDelta == 0 )
        return; // i think this is the indication that a trackpad gesture has 'finished'... on the iphone, i think it means 'stop animate scroll now'

    // if option key held down, zoom
    NSPoint focusPt = [self convertPoint:[event locationInWindow] fromView:NULL];
    if ( NSPointInRect(focusPt , miniMapRect) )
    {
        // // focus first
        // maybe too annoying... miniMapMouseCurrPt = NSMakePoint( focusPt.x - miniMapRect.origin.x, focusPt.y - miniMapRect.origin.y );
        //[self focusOnMiniMap];

        // zoom second
        allowAnimate = NO;
        [self zoom:totalDelta isSlider:NO isAbsolute:NO useMousePt:YES];

    }
    else if ( flags & NSAlternateKeyMask )
    {
        allowAnimate = NO;
        [self zoom:totalDelta isSlider:NO isAbsolute:NO useMousePt:YES];
    }
    else
    {
        double theDeltaPixels;
        theDeltaPixels = visRect.size.width / 10.0;

        if ( [event respondsToSelector:@selector(momentumPhase)] && [event momentumPhase] )
        {
            DLog( @"ignoring trackpad momentum gesture" );
        }
        else if ( isTouchEventActive )
        {
            if (flags & NSShiftKeyMask) // scroll left/right)
            {
                if ( totalDelta > 0 )
                    theDeltaPixels = -theDeltaPixels;
                visRect.origin.x += theDeltaPixels;
            }
            else
            {
                visRect.origin.x -= deltaX * visRect.size.width / 80.0;
                visRect.origin.y -= deltaY * visRect.size.height / 80.0;
            }
            //DLog( @"detaX %f y %f", deltaX, deltaY );
        }
        else
        {
            //DLog( @"detaX %f y %f total %f", deltaX, deltaY, totalDelta );
            if ( totalDelta > 0 )
                theDeltaPixels = -theDeltaPixels; // subtract time when scrolling up
            //DLog( @"RulerView scrollWheel delta %f deltaX %f deltaY %f deltaZ %f", theDeltaPixels, deltaX, deltaY, deltaZ );

            if (flags & NSShiftKeyMask) // scroll left/right
                visRect.origin.x += theDeltaPixels;
            else
                visRect.origin.y += theDeltaPixels;
        }


        [[self superview] scrollRectToVisible:visRect];

        [self savePersistentFocusPt:
         NSMakePoint( NSMidX([self visibleRect]),
                     NSMidY([self visibleRect] ) )];
    }
}