2012-02-21 35 views
0

我從我的應用程序有一個頁面有2個UIScrollBars對象我將它命名爲scrol1和scrol2,scrol1在scrol2的左邊我想實現這個;同步兩個UIScrollBar的

當滾動條2從右向左滾動(從左向右)時滾動條不滾動,當滾動條從上到下和從下到上滾動時滾動條與滾動條同時滾動。我怎麼能做到這一點。請提供一些例子或想法。 預先感謝您。

回答

0

在創建您的滾動視圖,你應該在方法創建一個鍵值觀察(志願)爲您的滾動視圖的contentOffset scrol2

NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld; 
[scrol2 addObserver:self forKeyPath:@"contentOffset" options:options context:nil]; 

然後觀察值,滾動根據您的scrollviews你希望的滾動行爲。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    CGPoint new = [[change objectForKey:@"new"] CGPointValue]; 
    CGPoint old = [[change objectForKey:@"old"] CGPointValue]; 
    // it could be necessary to convert your scroll offset for the other scroll view 
    [scrol1 setContentOffset:new]; 
}