2011-03-17 43 views
1

我想兩個UIScrollView添加的爲一個UIView,無論是scrollView的顯示正常,是我遇到的問題是,如何確定哪些scrollView滾動,因爲根據我有填充圖像。這是我在做什麼:兩個UIScrollView中的一個的UIView

我有一個ViewControllerUIScrollViewDelegate

在我ViewConrollerloadView方法,我做了以下內容:

CGRect scrollViewFrame1; 
CGPoint scrollViewPoint1; 
scrollViewPoint1.x = 0; 
scrollViewPoint1.y = 57; 

CGSize scrollViewSize1; 
scrollViewSize1.width = 320; 
scrollViewSize1.height = 154; 

scrollViewFrame1.size = scrollViewSize1; 
scrollViewFrame1.origin = scrollViewPoint1; 

CGRect scrollViewFrame2; 
CGPoint scrollViewPoint2; 
scrollViewPoint2.x = 0; 
scrollViewPoint2.y = 258; 

CGSize scrollViewSize2; 
scrollViewSize2.width = 320; 
scrollViewSize2.height = 154; 

scrollViewFrame2.size = scrollViewSize2; 
scrollViewFrame2.origin = scrollViewPoint2; 

scrollView1 = [[UIScrollView alloc] initWithFrame:scrollViewFrame1]; 
scrollView2 = [[UIScrollView alloc] initWithFrame:scrollViewFrame2]; 

然後:

scrollView1.delegate = self; 
    scrollView2.delegate = self; 

然後:

[self.view addSubView:scrollView1]; 
[self.view addSubView:scrollView2]; 

我有一個scrollViewDidScroll:方法,我該如何確定這個方法去哪個t所調用,因爲基於此,我需要爲我的scrollView填充不同的圖像。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
     //Code to populate the next or previous images for scrollView 
      // If it was one i am able to show the images 
} 

感謝您的幫助。

回答

9

委託方法將與它一起發送消息的對象(本例中爲UIScrollView)。所以,你所要做的就是檢查你的實例變量scrollView1和scrollView2。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (scrollView == scrollView1) { 
     //do stuff with scrollView1 
    } else if (scrollView == scrollView2) { 
     //do stuff with scrollView2 
    } 
} 
+0

感謝大衛快速回復。是的,這工作 – Yogesh 2011-03-17 16:01:35

+0

非常感謝這一點。 – shaikh 2012-12-25 12:48:44

0

FYI

您還可以設置兩個滾動視圖的標籤來區分

scrollView1.tag=10; 
scrollView2.tag=11; 
[self.view addSubView:scrollView1]; 
[self.view addSubView:scrollView2]; 

在您的代理

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (scrollView1.tag==10) { 
     //do stuff with scrollView1 
    } else if (scrollView2.tag==11) { 
     //do stuff with scrollView2 
    } 
} 
+0

謝謝,是的,這也工作得很好。 – Yogesh 2011-03-17 20:21:15

+0

但這不像@David Beck那樣乾淨。 – shaikh 2012-12-25 12:50:22

相關問題