多个UIScrollView

3
让我直截了当地说吧,这里有3个ScrollView(滚动视图),如附件所示。我对代理感到困惑,请看链接: 图片截图 它们分别是:
1. 滚动视图1 - 仅垂直滚动 2. 滚动视图2 - 可以自由滚动(水平/垂直) 3. 滚动视图3 - 仅水平滚动
我希望在我水平滚动“滚动视图2”时,“滚动视图1”也会随之滚动;当我垂直滚动“滚动视图2”时,“滚动视图3”也随之滚动。
如果有这方面的示例代码/实例就太好了。
祝好,
Sky.
1个回答

2

我已经做过类似于两个NSTableView的事情,一个的滚动会影响第二个的滚动。

以下是相关代码:

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

哇哦,原来实现滚动委托并不那么简单啊,我能不能只用像scrollviewdidscroll这样的东西呢? - discodisky
我花了近15天的时间搜索上述内容,最终得到了上面的代码。 - Anoop Vaidya
说实话,这让我想到可以使用TableView来替换Scroll-1。而Scroll-2是另一个带有滚动条的视图。但是我仍然对ScrollView感到困惑,因为它在Scroll-2和Scroll-3之间进行平行滚动,即使我想要垂直滚动ScrollTableView和Scroll-2。 - discodisky
嗯,我只是在思考,如果我使用tabelview,那么内容表格如何水平滚动并在scrollview-3上执行并行滚动? - discodisky
在我的代码中,我滚动了scrollView,我猜想类似的效果可以实现。 - Anoop Vaidya
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接