0

我有一个MTKView,我正在渲染一个三角形,我希望能够使用调用scrollWheel:方法的两指拖动手势来翻译它。scrollWheel:方法的默认行为是产生额外的滚动事件来实现随着时间推移而逐渐消失的平滑滚动。我想禁用该行为,并在我将手指从触控板上抬起时立即结束刚性滚动。有谁知道如何禁用NSView的平滑滚动?

4

2 回答 2

1

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

于 2016-01-18T19:55:52.193 回答
0

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

此外,通过忽略 0 的总 x+y+z 增量来防止跳跃!

- (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] ) )];
    }
}
于 2016-01-18T23:29:45.880 回答