Reputation: 39
let me straight to the point,
there was 3 scrollview like the attachment, and i'm confused for the delegate. see the link : image screenshoot
here it is :
and i want to make if i was scrolling "scroll 2" horizontally then the "scroll 1" was scroll too. and if i was scrolled "scroll 2" vertically then the "scroll 3" was scroll too.
it would nice if there's a sample code / example about this.
regards,
sky.
Upvotes: 3
Views: 231
Reputation: 46533
I did something similar to two NSTableViews, scrolling of one would make second scroll.
Here is the code for that:
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
Upvotes: 2