2013-03-14 133 views
1

我有兩個UIScrollViews,如果用戶在滾動視圖中滾動,我想讓其他滾動視圖也滾動。我已經閱讀了一個解決方案,其中包括將pangesturerecognizer從原始平移的滾動視圖傳遞到另一個滾動視圖,但如果我這樣做,原始滾動視圖根本不滾動。如何將滾動視圖從滾動視圖轉發到另一個滾動視圖

我發現,有一個委託方法

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 

但如果我嘗試掛鉤的scrollviews我的應用程序崩潰的pangesturerecognizers的代表。

希望有人能幫助我。

回答

1

你只需要更新第二滾動型

喜歡的東西的邊界: -

CGRect updateTheViewWithBounds = viewToUpdate.bounds; 
updateTheViewWithBounds.origin = scrolledView.contentOffset; 
viewToUpdate.bounds = updateTheViewWithBounds; 

這將做的工作。

至於從評論中可以看出,我會舉一個小例子。

創建兩個滾動視圖到的UIViewController

scrollOne = [[UIScrollView alloc] init]; 
    [scrollOne setBackgroundColor:[UIColor greenColor]]; 
    [scrollOne setFrame:CGRectMake(10, 20, 200, 300)]; 
    [scrollOne setContentSize:CGSizeMake(600, 600)]; 
    scrollOne.delegate = self; 

    scrollTwo = [[UIScrollView alloc] init]; 
    [scrollTwo setBackgroundColor:[UIColor redColor]]; 
    [scrollTwo setFrame:CGRectMake(230, 20, 200, 300)]; 
    [scrollTwo setContentSize:CGSizeMake(600, 600)]; 
    scrollTwo.delegate = self; 


    [self.view addSubview:scrollOne]; 
    [self.view addSubview:scrollTwo]; 

順應UIScrollView的代表和實現了相同的。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if([scrollView isEqual:scrollOne]) 
    { 
     CGRect updateTheViewWithBounds = scrollOne.bounds; 
     updateTheViewWithBounds.origin = scrollOne.contentOffset; 
     scrollTwo.bounds = updateTheViewWithBounds; 
     [scrollTwo flashScrollIndicators]; 
    } 
    else if([scrollView isEqual:scrollTwo]) 
    { 
     CGRect updateTheViewWithBounds = scrollTwo.bounds; 
     updateTheViewWithBounds.origin = scrollTwo.contentOffset; 
     scrollOne.bounds = updateTheViewWithBounds; 
     [scrollOne flashScrollIndicators]; 
    } 
} 

滾動任何上述scrollView將滾動scrollView。

+0

這隻會在一個滾動視圖沒有滾動時才起作用,對嗎? :( – kohaXun 2013-03-14 13:09:59

+0

每當第一個scrollview改變,在委託,更新相應的其他scrollView – 2013-03-14 13:41:47

+0

你是什麼意思?你的意思是scrollView:DidScroll委託方法嗎?你知道我可以如何使用這種方法,仍然能夠讓scrollviews以不同的速度滾動? – kohaXun 2013-03-15 08:32:01