_
viewdidload
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
我正在使用此代码在 ScrollView 中以不同的颜色显示分页,为了识别,我只想用图像而不是颜色替换它。我正在使用此代码
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"h1@2x"], [UIImage imageNamed:@"h2@2x"], [UIImage imageNamed:@"h2.1@2x"], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView*subview = [[UIView alloc] initWithFrame:frame];
UIImage *imggg = [colors objectAtIndex:i];
[subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]];
[self.scrollView addSubview:subview];
[subview release];
}
但是我只得到两个 View ,并且图像的大小不适合它被释放。如何使用上面的代码设置图像。请帮我解决这个问题。 提前致谢。
最佳答案
我通过将数组中图像的大小减小到 ScrollView 的大小来得到答案。我的 ScrollView 大小是 320,424,我将图像大小切片为 320,424。它工作完美。谢谢。
关于iphone - ScrollView 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8700737/