NSView 禁用平滑滚动
NSView disable smooth scrolling
我有一个 MTKView,我正在其中渲染一个三角形,我希望能够使用调用 的双指拖动手势来平移它滚动轮: 方法。 scrollWheel: 方法的默认行为是产生额外的滚动事件来实现 smooth 随着时间的推移逐渐消失的滚动。我想禁用该行为并进行刚性滚动,一旦我将手指从触控板上抬起,该滚动就会结束。有谁知道如何为 NSView 禁用平滑滚动?
可以查看NSEvent
参数的momentumPhase
属性来决定是否忽略该事件。
是的,检查 momentumPhase 很重要...此外,我添加了一个成员变量 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] ) )];
}
}
我有一个 MTKView,我正在其中渲染一个三角形,我希望能够使用调用 的双指拖动手势来平移它滚动轮: 方法。 scrollWheel: 方法的默认行为是产生额外的滚动事件来实现 smooth 随着时间的推移逐渐消失的滚动。我想禁用该行为并进行刚性滚动,一旦我将手指从触控板上抬起,该滚动就会结束。有谁知道如何为 NSView 禁用平滑滚动?
可以查看NSEvent
参数的momentumPhase
属性来决定是否忽略该事件。
是的,检查 momentumPhase 很重要...此外,我添加了一个成员变量 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] ) )];
}
}