私は 2 つの NSTableViews に似たようなことをしました.1 つをスクロールすると 2 つ目のスクロールになります。
そのためのコードは次のとおりです。
MyScrollView.h
#import <AppKit/AppKit.h>
#import "AppDelegate.h"
@interface MyScrollView : NSScrollView
@property (strong) AppDelegate *app;
@end
MyScrollView.m
#import "MyScrollView.h"
//#define LogRect(RECT) NSLog(@"%s: (%0.0f, %0.0f) %0.0f x %0.0f", #RECT, RECT.origin.x, RECT.origin.y, RECT.size.width, RECT.size.height)
@implementation MyScrollView
@synthesize app;
- (void)reflectScrolledClipView:(NSClipView *)aClipView{
[super reflectScrolledClipView:(NSClipView *)aClipView];
//post a notification
app=[[AppDelegate alloc] init];
if (app) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"scrolled" object:aClipView];
}
}
@end
AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSTableView *aTableView;
@property (strong) IBOutlet NSTableView *bTableView;
@property (strong) IBOutlet NSTableView *cTableView;
@property (strong) IBOutlet NSView *aView;
@property (strong) IBOutlet NSView *bView;
@end
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
/*
Unwanted methods are not shown here, like delegates, datasource, init etc
*/
-(void)scrollTable:(NSNotification *)aNotification{
NSPoint globalLocation = [NSEvent mouseLocation ];
NSPoint windowLocationForA = [[aView window ] convertScreenToBase:globalLocation ];
NSPoint viewLocationForA = [aView convertPoint: windowLocationForA fromView: nil ];
NSPoint windowLocationForB = [[bView window ] convertScreenToBase:globalLocation ];
NSPoint viewLocationForB = [bView convertPoint: windowLocationForB fromView: nil ];
if (NSPointInRect(viewLocationForA, [aView bounds])) { // i scrolled on A
NSLog(@"Scrolled A");
if ([[[aTableView superview]superview] isEqualTo:[[aNotification object] superview]]) {
[bTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
[cTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
}
}
else if (NSPointInRect(viewLocationForB, [bView bounds])) { // i scrolled on B
NSLog(@"Scrolled B");
if ([[[bTableView superview]superview] isEqualTo:[[aNotification object] superview]]){
[aTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
[cTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
}
}
else{ // i scrolled on C
NSLog(@"Scrolled C");
if ([[[cTableView superview]superview] isEqualTo:[[aNotification object] superview]]){
[aTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
[bTableView scrollRectToVisible:[[aNotification object] documentVisibleRect]];
}
}
}
-(void)awakeFromNib{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(scrollTable:)
name:@"scrolled"
object:nil];
}
@end