2011-12-24 39 views
3

我有一个NSScrollview嵌套在另一个NSScrollview。我如何使内部视图只处理水平滚动?垂直滚动应该移动外部视图。NSScrollView在另一个NSScrollView

目前我将scrollWheel:事件从内部视图传递到外部视图,但速度非常慢。

+0

iPhone或MacOS?我在猜测MacOS,因为你说的是​​NSScrollView而不是UIScrollView。 – 2011-12-24 09:48:12

+0

是的,Mac OS,有任何想法吗? – NeXT5tep 2011-12-24 10:32:50

回答

2

这是我所说的NSScrollView的子类。因为它仅仅是通过它不关心了响应链中的事件应该是为高性能,好像它是不是一个子类别(或至少接近)

.h文件

#import <Cocoa/Cocoa.h> 

@interface HorizontalScrollView : NSScrollView 

@end 

和m

@implementation HorizontalScrollView 

- (void)scrollWheel:(NSEvent *)theEvent { 
    NSLog(@"%@", theEvent); 
    if(theEvent.deltaX !=0) 
     [super scrollWheel:theEvent]; 
    else 
     [[self nextResponder] scrollWheel:theEvent]; 

} 
@end 
5

我也遇到了嵌套滚动视图的问题。内滚动视图应水平滚动,外滚动应垂直滚动。

在处理来自魔术鼠标/触控板的滚动事件时,仅为每个手势选择一个滚动视图非常重要,否则当手指不能完全直线移动时会看到奇怪的抖动。您还应该确保用两根手指敲击触控板会显示两个滚动条。

当使用老式滚动鼠标处理来自强大鼠标或鼠标的传统滚动事件时,您必须为每个事件选择正确的滚动视图,因为事件中没有手势相位信息。

这是我的子类内滚动视图,只在山狮测试:

@interface PGEHorizontalScrollView : NSScrollView { 
    BOOL currentScrollIsHorizontal; 
} 
@end 

@implementation PGEHorizontalScrollView 
-(void)scrollWheel:(NSEvent *)theEvent { 
    /* Ensure that both scrollbars are flashed when the user taps trackpad with two fingers */ 
    if (theEvent.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 (theEvent.phase == NSEventPhaseBegan || (theEvent.phase==NSEventPhaseNone && theEvent.momentumPhase==NSEventPhaseNone)) { 
     currentScrollIsHorizontal = fabs(theEvent.scrollingDeltaX) > fabs(theEvent.scrollingDeltaY); 
    } 
    if (currentScrollIsHorizontal) { 
     [super scrollWheel:theEvent]; 
    } else { 
     [[self nextResponder] scrollWheel:theEvent]; 
    } 
} 
@end 

我的实现并不总是向前的手势正确取消的事件,但至少在10.8这不会造成问题。

+2

大多数情况下,这对我来说是10.10的工作。我需要添加'+(BOOL)isCompatibleWithResponsiveScrolling {return YES; },因为NSScrollView检测到'-scrollWheel:'的覆盖,我认为在无响应的滚动中存在一个错误。 – joerick 2015-05-20 15:17:17