2011-12-24 42 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