NSView 禁用平滑滚动



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

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

是的,检查动量相位很重要...此外,我添加了一个成员var isTouchEventActive用于检测手势。

此外,通过忽略总 x+y+z 增量 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] ) )];
    }
}

相关内容

  • 没有找到相关文章

最新更新