Two UIScrollView in one UIView
I am trying to add two UIScrollView
's into one UIView
, Both the scrollView
's show up properly, the problem that I am having is how to determine which scrollView
is scrolled because based on that I have to populate the images. This is what I am doing开发者_运维问答:
I have a ViewController
with UIScrollViewDelegate
.
In the loadView
method of my ViewConroller
, I do the following:
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];
And then:
scrollView1.delegate = self;
scrollView2.delegate = self;
And then:
[self.view addSubView:scrollView1];
[self.view addSubView:scrollView2];
I have one scrollViewDidScroll:
method, how do I determine which scrollView
this method got invoked by, because based on that i need to populate different images for my 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
}
Thanks for the help.
Delegate methods send with it the object that sent the message (the UIScrollView in this case). So, all you have to do is check that against your instance variables of scrollView1 and scrollView2.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == scrollView1) {
//do stuff with scrollView1
} else if (scrollView == scrollView2) {
//do stuff with scrollView2
}
}
FYI
You can also set a tag for both of the scrollView to differentiate
scrollView1.tag=10;
scrollView2.tag=11;
[self.view addSubView:scrollView1];
[self.view addSubView:scrollView2];
In your delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView1.tag==10) {
//do stuff with scrollView1
} else if (scrollView2.tag==11) {
//do stuff with scrollView2
}
}
精彩评论