NSScrollView inside another NSScrollView

不想你离开。 提交于 2019-12-18 03:44:04

问题


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.


回答1:


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.




回答2:


This is my subclass of NSScrollView that does what you are asking. Since it is merely passing the events it doesn't care about up the responder chain it should be as performant as if it weren't a subclass (or at least close)

h file

#import <Cocoa/Cocoa.h>

@interface HorizontalScrollView : NSScrollView

@end

and m

@implementation HorizontalScrollView

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

}
@end



回答3:


Swift 4.

Answer is a translation of Jakob's excellent answer above, with the addition of a direction flag.

As mentioned in the comments on his answer, there can be subtle complications if this isn't done right. When simply forwarding all scrollWheel() calls to the next responder, I was seeing issues with NSTrackingAreas not being updated correctly and tooltips and cursor styles over NSTextView views being misaligned.

class ECGuidedScrollView: NSScrollView
{
    enum ScrollDirection {
        case vertical
        case horizontal
    }

    private var currentScrollMatches: Bool = false
    private var scrollDirection: ScrollDirection

    init(withDirection direction: ScrollDirection) {
        scrollDirection = direction
        super.init(frame: NSRect.zero)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func scrollWheel(with event: NSEvent)
    {        
        // Ensure that both scrollbars are flashed when the user taps trackpad with two fingers
        if event.phase == .mayBegin {
            super.scrollWheel(with: event)
            nextResponder?.scrollWheel(with: event)
            return
        }

        /*
         Check the scroll direction only at the beginning of a gesture for modern scrolling devices
         Check every event for legacy scrolling devices
         */

        if event.phase == .began || (event.phase == [] && event.momentumPhase == [])
        {
            switch scrollDirection {
            case .vertical:
                currentScrollMatches = fabs(event.scrollingDeltaY) > fabs(event.scrollingDeltaX)
            case .horizontal:
                currentScrollMatches = fabs(event.scrollingDeltaX) > fabs(event.scrollingDeltaY)
            }
        }

        if currentScrollMatches {
            super.scrollWheel(with: event)
        } else {
            self.nextResponder?.scrollWheel(with: event)
        }
    }
}


来源:https://stackoverflow.com/questions/8623785/nsscrollview-inside-another-nsscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!