我做了以下代码,在分页期间可以重用三个视图UIScrollView
以节省实时内存-->
#pragma mark - UIScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = self.view.frame.size.width;
CGPoint aContentOffSet = [[self scrollView] contentOffset] ;
float currPos = aContentOffSet.x;
int selectedPage = roundf(currPos/pageWidth);
[[self pageControl] setCurrentPage:selectedPage];
[self update:selectedPage];
}
#pragma mark - Custom methods
-(void)update:(int) selectedPage{
BOOL view1FrameShallBeUnchanged = false;
BOOL view2FrameShallBeUnchanged = false;
BOOL view3FrameShallBeUnchanged = false;
BOOL aFrame1Matched = false;
BOOL aFrame2Matched = false;
BOOL aFrame3Matched = false;
CGRect aFrame1 = CGRectMake(selectedPage*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
CGRect aFrame2 = CGRectMake((selectedPage-1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
CGRect aFrame3 = CGRectMake((selectedPage+1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
ViewOnScrollView *aView1 = (ViewOnScrollView*)[[self scrollView] viewWithTag:1234];
ViewOnScrollView *aView2 = (ViewOnScrollView*)[[self scrollView] viewWithTag:12345];
ViewOnScrollView *aView3 = (ViewOnScrollView*)[[self scrollView] viewWithTag:123456];
if(aView1 && aView2 && aView3){
//Check for Frame 1
if(aFrame1.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
else if(aFrame1.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
else if(aFrame1.origin.x ==aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
//Check for Frame 2
if(aFrame2.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
else if(aFrame2.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
else if(aFrame2.origin.x == aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
//Check for Frame 3
if(aFrame3.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
else if(aFrame3.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
else if(aFrame3.origin.x == aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
if(!view1FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView1 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView1 setFrame:aFrame2];
}
else{
[aView1 setFrame:aFrame3];
}
[self hideOrShowTheTabs:aView1];
[self hideShowView:aView1];
}
if(!view2FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView2 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView2 setFrame:aFrame2];
}
else{
[aView2 setFrame:aFrame3];
}
[self hideShowView:aView2];
}
if(!view3FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView3 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView3 setFrame:aFrame2];
}
else{
[aView3 setFrame:aFrame3];
}
[self hideShowView:aView3];
}
}
}
-(void)hideShowView:(ViewOnScrollView*)theView{
if(theView.frame.origin.x<0 || theView.frame.origin.x>[self.scrollView contentSize].width )
theView.hidden = YES;
else{
theView.hidden = NO;
}
}
欢迎评论/建议/更好的方法来做同样的事情。