我正在滚动视图中实现分页。具有大小 (259,263) 和大小 (741, 723) 的图像的滚动视图。到目前为止,我能够取得一点成功。图像是如此之大,以至于它相互重叠。我无法在图像视图上调整图像大小。这是我的代码:
- (void)setupPage
{
NSMutableArray *imageArray= [[NSMutableArray
alloc]initWithObjects:@"female.png",@"male.png",nil];
int nimages;
CGFloat cx = 0;
for (nimages = 0; nimages<[imageArray count]; nimages++)
{
NSString *imageName = [NSString stringWithFormat:@"%@",[imageArray objectAtIndex:nimages]];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil)
{
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage)
{
return;
}
CGFloat pageWidth = _scrollView.frame.size.width/2;
int page = floor((_scrollView.contentOffset.x - pageWidth/2 ) / pageWidth) + 1;
pageControl.currentPage = page;
}
我通过为我的图像视图提供静态大小来获得我的解决方案
这是我的代码 NSUInteger nimages;
for (nimages = 1; nimages <= [imageArray count]; nimages++)
{
NSString *imageName = [NSString stringWithFormat:@"%@",[imageArray
objectAtIndex:nimages-1]];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil)
{
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = nimages;
[scrollView addSubview:imageView];
[imageView release];
}
UIImageView *view = nil;
NSArray *subviews = [scrollView 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
[scrollView setContentSize:CGSizeMake(([imageArray count] * kScrollObjWidth), [scrollView
bounds].size.height)];