Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

cocoa - NSScrollView inside another NSScrollView

I have a NSScrollview nested inside another NSScrollview. How do i make the inner view handle horizontal scrolling only? Vertical scrolling should move the outer view.

Currently i pass the scrollWheel: event to the outer view from the inner view, but it is very slow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I also had the problem of nested scroll views. The inner scroll view should scroll horizontally, and the outer should scroll vertically.

When handling scroll events from magic mouse / trackpad, it is important to pick only one of the scroll views for each gesture, otherwise you will see odd jerking when your fingers don't move perfectly straight. You should also ensure that tapping the trackpad with two fingers shows both scrollers.

When handling legacy scroll events from mighty mouse or mice with old fashioned scroll wheels, you must pick the right scroll view for each event, because there is no gesture phase information in the events.

This is my subclass for the inner scroll view, tested only in Mountain Lion:

@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

My implementation does not always forward Gesture cancel events correctly, but at least in 10.8 this does not cause problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...