2011-07-31 16 views
2

我有这样的代码为滚动视图,以简易淋浴3个图像:IOS:UIScrollView中具有无限页面视图

const CGFloat kScrollObjHeight = 150.0; 
const CGFloat kScrollObjWidth = 320.0; 
const NSUInteger kNumImages = 3; 


- (void)layoutScrollImages 
{ 
UIImageView *view = nil; 
NSArray *subviews = [scrollView1 subviews]; 

// reposition all image subviews in a horizontal serial fashion 
CGFloat curXLoc = 0; 
for (view in subviews) 
{ 
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
    { 
     CGRect frame = view.frame; 
     frame.origin = CGPointMake(curXLoc, 0); 
     view.frame = frame; 

     curXLoc += (kScrollObjWidth); 
    } 
} 


// set the content size so it can be scrollable 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 

} 

- (void)viewDidLoad 
{ 
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

// 1. setup the scrollview for multiple images and add it to the view controller 
// 
// note: the following can be done in Interface Builder, but we show this in code for clarity 
[scrollView1 setBackgroundColor:[UIColor blackColor]]; 
[scrollView1 setCanCancelContentTouches:NO]; 
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
scrollView1.scrollEnabled = YES; 

// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
// if you want free-flowing scroll, don't set this property. 
scrollView1.pagingEnabled = YES; 
scrollView2.pagingEnabled = YES; 
scrollView3.pagingEnabled = YES; 

// load all the images from our bundle and add them to the scroll view 
NSUInteger i; 
for (i = 1; i <= kNumImages; i++) 
{ 
    NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect rect = imageView.frame; 
    rect.size.height = kScrollObjHeight; 
    rect.size.width = kScrollObjWidth; 
    imageView.frame = rect; 
    imageView.tag = i; // tag our images for later use when we place them in serial fashion 
    [scrollView1 addSubview:imageView]; 
    //[scrollView2 addSubview:imageView]; 
    //[scrollView3 addSubview:imageView]; 
    [imageView release]; 
} 

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview 

} 

但现在我想做一个滚动型是在最后的图像时,给我的第一图像后滚动视图和同样的事情,当我有第一个图像,如果我回去,它必须显示最后一个图像;所以我想创建一个寻呼循环。

回答

7

其基本思路是将自己设置为UIScrollViewDelegate,并将滚动位置的一些模运算应用于滚动位置。

这个想法有两个基本的变化。假设你的图像是A,B,C,所以你现在把它们放在按ABC排列的scrollview中。

在更合乎逻辑的解决方案 - 尤其是如果你有很多很多的图像 - 你看滚动的位置,一旦它到达的位置,视图被推向右,C已离开屏幕,你将图像重新排列为CAB,并将当前滚动位置向右移动一点以便该移动对用户不可见。换句话说,滚动位置被限制在两个屏幕区域,以B的中间为中心(所以,你可以得到B的全部和一半的屏幕)。每当你将它从左边的某个地方包装到右边的某个地方时,你将所有的图像视图向右移动一个位置。反之亦然。

在稍微容易实现的变体中,不是创建一个带有排列ABC的图像的滚动视图,而是安排为CABCA。然后应用相同的环绕逻辑,但应用到四个屏幕的中心区域,并且不要进行任何视图重组。

请确保您只使用setContentOffset:(或点符号,如scrollView.contentOffset =)作为设置者。 setContentOffset:animated:会否定速度。

+0

你好,谢谢你的回答。我希望你能提供更多的细节。我应该听什么委托方法来决定何时更改图像的顺序并更改内容偏移量?参考您提供的第一个解决方案。 – jmurphy

+1

scrollviewDidScroll:是最合适的 - 每当用户更改contentOffset时它都会给你留言。 – Tommy

+0

谢谢。我现在正在侦听scrollViewDidScroll并将我的contentSize设置为3 * sizeOfContent。当用户滚动时,我检查内容偏移量是0还是640(我的内容是320)。如果0用户向左滚动,则重新排序contentArray一个位置并将setContentOffset设置为320.如果偏移量为640,则反向。我还设置了bounce = NO,因此这些偏移量只能在每次滑动时命中一次。这听起来像是一种合理的方法,基本上是你想到的吗? – jmurphy

相关问题