我有這樣的代碼爲滾動視圖,以簡易淋浴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
}
但現在我想做一個滾動型是在最後的圖像時,給我的第一圖像後滾動視圖和同樣的事情,當我有第一個圖像,如果我回去,它必須顯示最後一個圖像;所以我想創建一個尋呼循環。
你好,謝謝你的回答。我希望你能提供更多的細節。我應該聽什麼委託方法來決定何時更改圖像的順序並更改內容偏移量?參考您提供的第一個解決方案。 – jmurphy
scrollviewDidScroll:是最合適的 - 每當用戶更改contentOffset時它都會給你留言。 – Tommy
謝謝。我現在正在偵聽scrollViewDidScroll並將我的contentSize設置爲3 * sizeOfContent。當用戶滾動時,我檢查內容偏移量是0還是640(我的內容是320)。如果0用戶向左滾動,則重新排序contentArray一個位置並將setContentOffset設置爲320.如果偏移量爲640,則反向。我還設置了bounce = NO,因此這些偏移量只能在每次滑動時命中一次。這聽起來像是一種合理的方法,基本上是你想到的嗎? – jmurphy